Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android AIDL實踐之清理應用緩存

android AIDL實踐之清理應用緩存

編輯:關於Android編程

1、把下面兩個aidl文件放在自己的工程中,自己的項目視為客戶端,來實現跨進程通信。

代碼如下:

建立包名:

/*
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

package android.content.pm;

import android.content.pm.PackageStats;
/**
 * API for package data change related callbacks from the Package Manager.
 * Some usage scenarios include deletion of cache directory, generate
 * statistics related to code, data, cache usage(TODO)
 * {@hide}
 */
oneway interface IPackageStatsObserver {
    
    void onGetStatsCompleted(in PackageStats pStats, boolean succeeded);
}


/* //device/java/android/android/view/WindowManager.aidl
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/

package android.content.pm;

parcelable PackageStats;


2、在activity界面利用Java反射實現

package com.example.yqqmobilesafe.cleanache;

import java.lang.reflect.Method;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PackageStats;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.text.format.Formatter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.yqqmobilesafe.R;
/**
 * 清理緩存
 * @author yqq
 *
 */
public class CleanCacheActivity extends Activity {
private PackageManager pm;
private LinearLayout mContainer;//用來存放待清理垃圾的應用
private ProgressBar mProgressBar;//顯示掃描的進度
private TextView tvScanAppName;//應用名
private final int SCANING_FINISH=1;//掃描應用結束
private final int SCANNING=2;//掃描中
//private MyPackageInfo info;
private Handler mHandler=new Handler(){

	@Override
	public void handleMessage(Message msg) {
		switch (msg.what) {
		case SCANING_FINISH:
			tvScanAppName.setText("掃描完成");
			break;
	case SCANNING:
		//返回應用的信息
		final MyPackageInfo info=(MyPackageInfo) msg.obj;
		//獲得應用名
		//String appName=info.packName;
		tvScanAppName.setText("正在掃描:"+info.packName);
		if(info.cacheSize>0){
			//將應用展示在界面容器上
			View view=View.inflate(CleanCacheActivity.this,R.layout.app_cache_info_item,null);
			//設置事件監聽
			view.setClickable(true);
			view.setBackgroundResource(R.drawable.ache_clear_item_bg_selector);
			//清空緩存數據
			view.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// 開啟設置界面
					Intent intent = new Intent();
					intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
					intent.setData(Uri.parse("package:"
							+info.packName ));
					startActivity(intent);
					
				}
			});
			ImageView appIconIV=(ImageView) view.findViewById(R.id.iv_app_icon);
			TextView appNameTV=(TextView) view.findViewById(R.id.tv_app_name);
			TextView appCacheSizeTV=(TextView) view.findViewById(R.id.tv_app_cache_size);
			TextView appDataSizeTV=(TextView) view.findViewById(R.id.tv_app_code_size);
			//設置顯示數據
			try {
				appIconIV.setImageDrawable(pm.getApplicationInfo(
						info.packName, 0).loadIcon(pm));
			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			appNameTV.setText(info.packName);
			appCacheSizeTV.setText("緩存大小:"+Formatter.formatFileSize(getApplicationContext(), info.cacheSize));
			appDataSizeTV.setText("數據大小"+Formatter.formatFileSize(getApplicationContext(), info.dataSize));
			mContainer.addView(view,0);
		}
		
		
		break;
		}
		
		super.handleMessage(msg);
	}
	
	
};


	public CleanCacheActivity() {
		
		
		
	}
	@Override
		protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_cache_clear);
		init();
			super.onCreate(savedInstanceState);
		}
		
	/**
	 * 初始化信息
	 */
	private void init() {
		mContainer=(LinearLayout) this.findViewById(R.id.ll_container);
		tvScanAppName=(TextView) this.findViewById(R.id.tv_scaning_app_name);
		mProgressBar=(ProgressBar) this.findViewById(R.id.pb);
		
		pm=getPackageManager();
		scanAppCache();
		
	}
	/**
	 * 掃描應用獲得待清理的應用
	 */
	private void scanAppCache(){
		new Thread(new Runnable() {
			
		

			@Override
			public void run() {
				List infos=pm.getInstalledPackages(0);//獲得已經安裝應用的信息
				mProgressBar.setMax(infos.size());//設置進度條的最大數目
				int total=0;
				for(PackageInfo info:infos){
					getPackageSize(info.packageName);
					try {
						Thread.sleep(300);
						total++;
						mProgressBar.setProgress(total);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				
					
				}
				Message message=Message.obtain();
				message.what=SCANING_FINISH;//掃描完成
				mHandler.sendMessage(message);
				
			}
		}).start();
	
		
		
	}
	
	
	/**
	 * 通過反射獲得應用的大小
	 * @param packName應用的包名
	 */
	private void getPackageSize(String packName){
	
				try {
					Method method=PackageManager.class.getMethod("getPackageSizeInfo", new Class[]{String.class,IPackageStatsObserver.class});
					method.invoke(pm,new Object[]{packName,new MyObserver(packName)});
					
				 
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
	
		
	}
	
	
	
	/**
	 * 	定義一個包狀態觀察者獲得數據緩存,代碼大小,數據大小
	 * @author yqq
	 *
	 */
	private class MyObserver extends IPackageStatsObserver.Stub{
		private String packName;//應用的包名
		
		
		public MyObserver(String packName) {
			super();
			this.packName = packName;
		}


		@Override
		public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
				throws RemoteException {
			//獲得每個應用的大小
			MyPackageInfo myPackageInfo=new MyPackageInfo();
			myPackageInfo.dataSize=pStats.dataSize;
			myPackageInfo.cacheSize=pStats.cacheSize;
			myPackageInfo.packName=packName;
			Message message=Message.obtain();
			message.obj=myPackageInfo;
			message.what=SCANNING;
			mHandler.sendMessage(message);
			myPackageInfo=null;
		}


		
		
		
	}
	
	private class MyPackageInfo{
		String packName;//應用的包名
		long dataSize;//數據大小
		long cacheSize;//緩存大小
	}
	

}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved