我的手机管家(15) 缓存处理 逻辑代码
private PackageManager pm;//包管理器
//获取包管理器
pm = getPackageManager();
使用子线程获取应用程序的整体包信息, PackageInfo 存储包的整体信息
List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
//开始遍历每个包,获取图标,名称, 大小 等信息
for(PackageInfo pi : packages){
//通过packageInfo 来获取应用包名
String packagename = pi.applicationInfo.loadLoacl(pm).toString();
//通过反射, 获取缓存大小 //从PackageManager中获取方法名getPackageSizeInfo,
参数列表的类型是String.class,IPackageStatsObserver.class, 获取方法
Method method = pm.getClass().
getMethod("getPackageSizeInfo", String.class,IPackageStatsObserver.class);
//调用方法
method.invoke(pm, packageName, new MyStatsObserver());
}
使用进程通信
extends IPackageStatsObserver.Stub, 获取包的具体信息
/** * */ class MyStatsObserver extends IPackageStatsObserver.Stub{ //在子线程运行 @Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { //缓存大小 long cacheSize = pStats.cacheSize; //安装包大小 long codeSize = pStats.codeSize; //数据包大小 long dataSize = pStats.dataSize; if(cacheSize > 0 ){ //获取图标,名称,缓存大小 String packageName=pStats.packageName; try { ApplicationInfo app = pm.getApplicationInfo(packageName, 0); CacheInfo info = new CacheInfo(); info.name = app.loadLabel(pm).toString(); info.icon = app.loadIcon(pm); info.cacheSize = cacheSize; info.packageName = packageName; //通知更新界面 Message message = Message.obtain(); message.what = 2;//有缓存时发消息 message.obj = info; myHandler.sendMessage(message); } catch (Exception e) { e.printStackTrace(); } } } }
/** * 扫描 */ public void doScan() { new Thread(new Runnable() { @Override public void run() { //获取已安装的应用程序的信息, packageInfo存储包的整体内容信息 List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES); progressBar.setMax(packages.size()); int progress = 0; //遍历每个包信息 for(PackageInfo info : packages){ //应用名称 String packageName = info.applicationInfo.loadLabel(pm).toString(); try { //通过反射, 获取缓存大小 Method method = pm.getClass(). getMethod("getPackageSizeInfo", String.class,IPackageStatsObserver.class); //调用方法 method.invoke(pm, packageName, new MyStatsObserver()); progress++; progressBar.setProgress(progress); //更新进度 Message msg = Message.obtain(); msg.what = 1; msg.obj = packageName; myHandler.sendMessage(msg); //休眠 Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } } Message msg = Message.obtain(); myHandler.sendEmptyMessage(0); } }).start(); }
package com.chb.myphonesave.activity; import java.lang.reflect.Method; import java.util.List; import android.app.Activity; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.IPackageDataObserver; import android.content.pm.IPackageStatsObserver; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; 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.view.Window; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; import com.chb.myphonesave.R; import com.chb.myphonesave.entity.CacheInfo; /** * 缓存清理 * 1 正在扫描 * 2 扫描完成 * */ public class CacheCleanActivity extends Activity { boolean isScanOver;//是否是已扫描完成; TextView tv_tip;//显示正在扫描应用的名称 TextView tv1; //正在扫描 ProgressBar progressBar; LinearLayout cacheLayout; private PackageManager pm;//包管理器 Button btn_control; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_cacheclean); initView(); } /** * 初始化view */ private void initView() { tv_tip = (TextView) findViewById(R.id.acc_tv1); btn_control = (Button) findViewById(R.id.acc_btn1); progressBar = (ProgressBar) findViewById(R.id.progressBar1); tv1 = (TextView) findViewById(R.id.acc_tv2); cacheLayout = (LinearLayout) findViewById(R.id.cache_linear); pm = getPackageManager(); } /** * 扫描按钮事件监听 * * 点击按钮,清空缓存 :立即清理全部缓存 需要权限:android.permission.CLEAR_APP_CACHE * @param v */ public void onControl(View v){ if(!isScanOver){//点击开始扫描 doScan(); }else {//扫描完成,一键清理 doCleanCache(); } } public void doCleanCache() { //通过反射,获得缓存大小 Method method; try { /** * 向系统请求尽更多空间,系统为了凑空间,就只能清理缓存,间接达到清理全部缓存的目的 */ method = pm.getClass().getMethod("freeStorageAndNotify", long.class,IPackageDataObserver.class); method.invoke(pm, Long.MAX_VALUE,new IPackageDataObserver.Stub() { public void onRemoveCompleted(String packageName, boolean succeeded) throws RemoteException { System.out.println("是否清理缓存成功:"+succeeded); } }); } catch (Exception e) { e.printStackTrace(); } btn_control.setText("完成"); } /** * 扫描 */ public void doScan() { new Thread(new Runnable() { @Override public void run() { //获取已安装的应用程序的信息, packageInfo存储包的整体内容信息 List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES); progressBar.setMax(packages.size()); int progress = 0; //遍历每个包信息 for(PackageInfo info : packages){ //应用名称 String packageName = info.applicationInfo.loadLabel(pm).toString(); try { //通过反射, 获取缓存大小 Method method = pm.getClass(). getMethod("getPackageSizeInfo", String.class,IPackageStatsObserver.class); //调用方法 method.invoke(pm, packageName, new MyStatsObserver()); progress++; progressBar.setProgress(progress); //更新进度 Message msg = Message.obtain(); msg.what = 1; msg.obj = packageName; myHandler.sendMessage(msg); //休眠 Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } } Message msg = Message.obtain(); myHandler.sendEmptyMessage(0); } }).start(); } /** * 处理缓存处理的进程 */ private Handler myHandler = new Handler() { public void handleMessage(Message msg) { if(msg.what == 1){//正在扫描, 每次更新扫描信息, tv_tip.setText("正在扫描"+msg.obj); }else if(msg.what == 0){ tv_tip.setText("扫描完毕"); btn_control.setText("一键清理"); tv1.setText("扫描完毕"); isScanOver = true; }else if(msg.what == 2){//有缓存 final CacheInfo info = (CacheInfo) msg.obj; View view = View.inflate(getApplicationContext(), R.layout.cache_info, null); ImageView icon = (ImageView) view.findViewById(R.id.cache_iv_icon); ImageView delete=(ImageView) view.findViewById(R.id.cache_iv_delete); TextView tvTitle=(TextView) view.findViewById(R.id.cache_tv_title); TextView tvCache=(TextView) view.findViewById(R.id.cache_tv_cache_size); //设置每个缓存应用的信息 icon.setImageDrawable(info.icon); tvTitle.setText(info.name); tvCache.setText("缓存大小为:"+Formatter.formatFileSize(getApplicationContext(), info.cacheSize)); cacheLayout.addView(view,0); delete.setOnClickListener( new OnClickListener() { public void onClick(View v) { //清理单个缓存,跳转到系统设置界面 Intent intent = new Intent(); intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS"); intent.setData(Uri.parse("package:"+info.packageName)); intent.addCategory(Intent.CATEGORY_DEFAULT); startActivity(intent); } }); } }; }; /** * */ class MyStatsObserver extends IPackageStatsObserver.Stub{ //在子线程运行 @Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { //缓存大小 long cacheSize = pStats.cacheSize; //安装包大小 long codeSize = pStats.codeSize; //数据包大小 long dataSize = pStats.dataSize; if(cacheSize > 0 ){ //获取图标,名称,缓存大小 String packageName=pStats.packageName; try { ApplicationInfo app = pm.getApplicationInfo(packageName, 0); CacheInfo info = new CacheInfo(); info.name = app.loadLabel(pm).toString(); info.icon = app.loadIcon(pm); info.cacheSize = cacheSize; info.packageName = packageName; //通知更新界面 Message message = Message.obtain(); message.what = 2;//有缓存时发消息 message.obj = info; myHandler.sendMessage(message); } catch (Exception e) { e.printStackTrace(); } } } } }
时间: 2024-11-10 13:49:11