码迷,mamicode.com
首页 > 其他好文 > 详细

37.缓存清理

时间:2015-12-01 16:26:08      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

CleanCacheActivity

  • 反射调用系统隐藏的方法
  • 不能自己清理缓冲,需要系统界面去删除,但是系统有个漏洞,当告诉系统清除的数据大于系统的内存的时候,系统会全部清理
  1. publicclassCleanCacheActivityextendsActivity{
  2. protectedstaticfinalint SCANING =1;
  3. publicstaticfinalint SHOW_CACHE_INFO =2;
  4. protectedstaticfinalint SCAN_FINISH =3;
  5. privateProgressBar progressBar1;
  6. privateLinearLayout ll_container;
  7. privateTextView tv_status;
  8. privatePackageManager pm;
  9. privateHandler handler =newHandler(){
  10. publicvoid handleMessage(android.os.Message msg){
  11. switch(msg.what){
  12. case SCANING:
  13. String text =(String) msg.obj;
  14. tv_status.setText("正在扫描:"+text);
  15. break;
  16. case SHOW_CACHE_INFO:
  17. View view =View.inflate(getApplicationContext(), R.layout.list_appcache_item,null);
  18. ImageView iv =(ImageView) view.findViewById(R.id.iv_icon);
  19. TextView tv_name =(TextView) view.findViewById(R.id.tv_name);
  20. TextView tv_cache =(TextView) view.findViewById(R.id.tv_cache);
  21. finalCacheInfo info =(CacheInfo) msg.obj;
  22. iv.setImageDrawable(info.icon);
  23. tv_name.setText(info.name);
  24. tv_cache.setText("缓存大小:"+Formatter.formatFileSize(getApplicationContext(), info.size));
  25. ImageView iv_delete =(ImageView) view.findViewById(R.id.iv_delete);
  26. iv_delete.setOnClickListener(newOnClickListener(){
  27. @Override
  28. publicvoid onClick(View v){
  29. Method[] methods =PackageManager.class.getMethods();
  30. for(Method method : methods){
  31. try{
  32. if("deleteApplicationCacheFiles".equals(method.getName())){
  33. method.invoke(pm, info.packname,newIPackageDataObserver.Stub(){
  34. @Override
  35. publicvoid onRemoveCompleted(String packageName,boolean succeeded)
  36. throwsRemoteException{
  37. }
  38. });
  39. }
  40. }catch(Exception e){
  41. Intent intent =newIntent();
  42. intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
  43. intent.addCategory(Intent.CATEGORY_DEFAULT);
  44. intent.setData(Uri.parse("package:"+info.packname));
  45. startActivity(intent);
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50. });
  51. ll_container.addView(view,0);
  52. break;
  53. case SCAN_FINISH:
  54. tv_status.setText("扫描完毕");
  55. break;
  56. }
  57. };
  58. };
  59. @Override
  60. protectedvoid onCreate(Bundle savedInstanceState){
  61. super.onCreate(savedInstanceState);
  62. setContentView(R.layout.activity_clean_cache);
  63. progressBar1 =(ProgressBar) findViewById(R.id.progressBar1);
  64. ll_container =(LinearLayout) findViewById(R.id.ll_container);
  65. tv_status =(TextView) findViewById(R.id.tv_status);
  66. newThread(){
  67. publicvoid run(){
  68. pm = getPackageManager();
  69. List<PackageInfo> packageInfos = pm
  70. .getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
  71. progressBar1.setMax(packageInfos.size());
  72. int total =0;
  73. for(PackageInfo packinfo: packageInfos){
  74. try{
  75. String packname = packinfo.packageName;
  76. Method method =PackageManager.class.getMethod("getPackageSizeInfo",String.class,IPackageStatsObserver.class);
  77. //第一个参数:哪个用这个方法,第二个参数:是可变参数,方法里的参数,第三个参数:接口AIDL,需要拷到自己项目里,包名必须和以前一样,然后会在gen目录自动生成文件去调用
  78. method.invoke(pm, packname,newMyObserver());
  79. Message msg =Message.obtain();
  80. msg.what= SCANING;
  81. msg.obj = packinfo.applicationInfo.loadLabel(pm).toString();
  82. handler.sendMessage(msg);
  83. }catch(Exception e){
  84. e.printStackTrace();
  85. }
  86. total ++;
  87. progressBar1.setProgress(total);
  88. try{
  89. Thread.sleep(50);
  90. }catch(InterruptedException e){
  91. e.printStackTrace();
  92. }
  93. }
  94. Message msg =Message.obtain();
  95. msg.what = SCAN_FINISH;
  96. handler.sendMessage(msg);
  97. };
  98. }.start();
  99. }
  100. privateclassMyObserverextendsIPackageStatsObserver.Stub{
  101. //这个是AIDL接口
  102. @Override
  103. publicvoid onGetStatsCompleted(PackageStats pStats,boolean succeeded)
  104. throwsRemoteException{
  105. long cache = pStats.cacheSize;
  106. long codeSize = pStats.codeSize;
  107. if(cache>0){
  108. //System.out.println("当前应用程序:"+pStats.packageName+"有缓存:"+Formatter.formatFileSize(getApplicationContext(), cache));
  109. try{
  110. Message msg =Message.obtain();
  111. msg.what = SHOW_CACHE_INFO;
  112. CacheInfo cacheInfo =newCacheInfo();
  113. cacheInfo.packname = pStats.packageName;
  114. cacheInfo.icon = pm.getApplicationInfo(pStats.packageName,0).loadIcon(pm);
  115. cacheInfo.name = pm.getApplicationInfo(pStats.packageName,0).loadLabel(pm).toString();
  116. cacheInfo.size = cache;
  117. msg.obj = cacheInfo;
  118. handler.sendMessage(msg);
  119. }catch(Exception e){
  120. e.printStackTrace();
  121. }
  122. }
  123. }
  124. }
  125. classCacheInfo{
  126. Drawable icon;
  127. String name;
  128. long size;
  129. String packname;
  130. }
  131. publicvoid cleanAll(View view){
  132. // /freeStorageAndNotify
  133. Method[] methods =PackageManager.class.getMethods();
  134. for(Method method:methods){
  135. if("freeStorageAndNotify".equals(method.getName())){
  136. try{
  137. method.invoke(pm,Integer.MAX_VALUE,newIPackageDataObserver.Stub(){
  138. @Override
  139. publicvoid onRemoveCompleted(String packageName,
  140. boolean succeeded)throwsRemoteException{
  141. System.out.println(succeeded);
  142. }
  143. });
  144. }catch(Exception e){
  145. e.printStackTrace();
  146. }
  147. return;
  148. }
  149. }
  150. }
  151. }
 





附件列表

 

37.缓存清理

标签:

原文地址:http://www.cnblogs.com/liuyu0529/p/5010345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!