android AIDL实践之清理应用缓存

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<PackageInfo> 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;//缓存大小
	}
	

}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。