标签:
CleanCacheActivity
publicclassCleanCacheActivityextendsActivity{protectedstaticfinalint SCANING =1;publicstaticfinalint SHOW_CACHE_INFO =2;protectedstaticfinalint SCAN_FINISH =3;privateProgressBar progressBar1;privateLinearLayout ll_container;privateTextView tv_status;privatePackageManager pm;privateHandler handler =newHandler(){publicvoid handleMessage(android.os.Message msg){switch(msg.what){case SCANING:String text =(String) msg.obj; tv_status.setText("正在扫描:"+text);break;case SHOW_CACHE_INFO:View view =View.inflate(getApplicationContext(), R.layout.list_appcache_item,null);ImageView iv =(ImageView) view.findViewById(R.id.iv_icon);TextView tv_name =(TextView) view.findViewById(R.id.tv_name);TextView tv_cache =(TextView) view.findViewById(R.id.tv_cache);finalCacheInfo info =(CacheInfo) msg.obj; iv.setImageDrawable(info.icon); tv_name.setText(info.name); tv_cache.setText("缓存大小:"+Formatter.formatFileSize(getApplicationContext(), info.size));ImageView iv_delete =(ImageView) view.findViewById(R.id.iv_delete); iv_delete.setOnClickListener(newOnClickListener(){@Overridepublicvoid onClick(View v){Method[] methods =PackageManager.class.getMethods();for(Method method : methods){try{if("deleteApplicationCacheFiles".equals(method.getName())){ method.invoke(pm, info.packname,newIPackageDataObserver.Stub(){@Overridepublicvoid onRemoveCompleted(String packageName,boolean succeeded)throwsRemoteException{}});}}catch(Exception e){Intent intent =newIntent(); intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setData(Uri.parse("package:"+info.packname)); startActivity(intent); e.printStackTrace();}}}}); ll_container.addView(view,0);break;case SCAN_FINISH: tv_status.setText("扫描完毕");break;}};};@Overrideprotectedvoid onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.activity_clean_cache); progressBar1 =(ProgressBar) findViewById(R.id.progressBar1); ll_container =(LinearLayout) findViewById(R.id.ll_container); tv_status =(TextView) findViewById(R.id.tv_status);newThread(){publicvoid run(){ pm = getPackageManager();List<PackageInfo> packageInfos = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES); progressBar1.setMax(packageInfos.size());int total =0;for(PackageInfo packinfo: packageInfos){try{String packname = packinfo.packageName;Method method =PackageManager.class.getMethod("getPackageSizeInfo",String.class,IPackageStatsObserver.class);//第一个参数:哪个用这个方法,第二个参数:是可变参数,方法里的参数,第三个参数:接口AIDL,需要拷到自己项目里,包名必须和以前一样,然后会在gen目录自动生成文件去调用
method.invoke(pm, packname,newMyObserver());Message msg =Message.obtain(); msg.what= SCANING; msg.obj = packinfo.applicationInfo.loadLabel(pm).toString(); handler.sendMessage(msg);}catch(Exception e){ e.printStackTrace();} total ++; progressBar1.setProgress(total);try{Thread.sleep(50);}catch(InterruptedException e){ e.printStackTrace();}}Message msg =Message.obtain(); msg.what = SCAN_FINISH; handler.sendMessage(msg);};}.start();}privateclassMyObserverextendsIPackageStatsObserver.Stub{//这个是AIDL接口@Overridepublicvoid onGetStatsCompleted(PackageStats pStats,boolean succeeded)throwsRemoteException{long cache = pStats.cacheSize;long codeSize = pStats.codeSize;if(cache>0){//System.out.println("当前应用程序:"+pStats.packageName+"有缓存:"+Formatter.formatFileSize(getApplicationContext(), cache));try{Message msg =Message.obtain(); msg.what = SHOW_CACHE_INFO;CacheInfo cacheInfo =newCacheInfo(); cacheInfo.packname = pStats.packageName; cacheInfo.icon = pm.getApplicationInfo(pStats.packageName,0).loadIcon(pm); cacheInfo.name = pm.getApplicationInfo(pStats.packageName,0).loadLabel(pm).toString(); cacheInfo.size = cache; msg.obj = cacheInfo; handler.sendMessage(msg);}catch(Exception e){ e.printStackTrace();}}}}classCacheInfo{Drawable icon;String name;long size;String packname;}publicvoid cleanAll(View view){// /freeStorageAndNotifyMethod[] methods =PackageManager.class.getMethods();for(Method method:methods){if("freeStorageAndNotify".equals(method.getName())){try{ method.invoke(pm,Integer.MAX_VALUE,newIPackageDataObserver.Stub(){@Overridepublicvoid onRemoveCompleted(String packageName,boolean succeeded)throwsRemoteException{System.out.println(succeeded);}});}catch(Exception e){ e.printStackTrace();}return;}}}}
标签:
原文地址:http://www.cnblogs.com/liuyu0529/p/5010345.html