[安卓]手机管家(二十) 缓存清理
来源:互联网 发布:网络推广职位描述 编辑:程序博客网 时间:2024/04/30 07:21
来一个生成缓存的应用
在cachedir下写点东西就好
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="77dp" android:text="Button" android:onClick="makecache"/>
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getCache();}public void makecache(View v){File file = new File(getCacheDir()+"/cache.txt"); try {FileWriter fw = new FileWriter(file);fw.write("hello world");fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//获取当前缓存信息public void getCache(){}}
用到反射知识
这里还需要aidl,注意包名
获得缓存信息
//获取当前缓存信息public void getCache(){PackageManager pm = getPackageManager();try {//获得字节码Class<?> loadclass = MainActivity.class.getClassLoader().loadClass("android.content.pm.PackageManager"); Method method = loadclass.getMethod("getPackageSizeInfo", String.class,IPackageStatsObserver.class); method.invoke(pm, "com.example.makecache",new MyPackageManagerObserver());} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}class MyPackageManagerObserver extends IPackageStatsObserver.Stub{@Overridepublic void onGetStatsCompleted(PackageStats pStats, boolean succeeded)throws RemoteException {// TODO Auto-generated method stublong cache = pStats.cacheSize; //bytelong data = pStats.dataSize;long code = pStats.codeSize;String cacheString = Formatter.formatFileSize(getApplicationContext(), cache);String dataString = Formatter.formatFileSize(getApplicationContext(), data);String codeString = Formatter.formatFileSize(getApplicationContext(), code);System.out.println("MainActivity.MyPackageManagerObserver.onGetStatsCompleted()"+"cache: "+cacheString+"data: "+dataString+"code: "+codeString);}}
我们是要PackageManager下的getPackageSizeInfo方法,但是这个方法比较敏感,有一个hidden标签,是被隐藏的,不能通过常用的调用方法去获取,所以需要反射,通过他的字节码去找到他,用到了getmethod方法,然后通过invoke去调用,第二个参数表示包名,第三个参数表示一个observer,这个observer里有一个callback,前面你获取到包名什么的,我来计算,所以在手机上打开setting里的APP查看缓存信息时,过一会才有,是在计算信息
权限 获得缓存信息是比较敏感的,只有通过反射才能搞到,权限是必须的
<uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/>
以上是demo,现在来做做看
关于布局,其中有一个进度条ProgressBar,一半默认是圆圈,这里做成长条形,需要定义其风格
另外 layer-list
将多个图片或上面两种效果按照顺序层叠起来,这需要单独定义一个XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="60dp" android:text="缓存清理" android:background="#00FF00" android:gravity="center" android:textSize="25sp"/> <ProgressBar android:id="@+id/pb_clearcache_scan" android:progressDrawable="@drawable/progress_horizontal" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/tv_cacheclear_scanning" android:singleLine="true"/> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="清理缓存" android:onClick="clearall" android:visibility="gone" android:layout_gravity="center_horizontal" android:id="@+id/bt_cacheclear_clearall"/> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/lv_clearcache_cachelist"/> </LinearLayout>在drawable中定义的layer-list
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <bitmap android:src="@drawable/security_progress_bg"/> </item> <item android:id="@android:id/secondaryProgress"> <bitmap android:src="@drawable/security_progress"/> </item> <item android:id="@android:id/progress"> <bitmap android:src="@drawable/security_progress"/> </item> </layer-list>
CleanCacheActivity架子
public class CleanCacheActivity extends Activity {private ProgressBar pb_clearcache_scan;private ListView lv_clearcache_cachelist;private TextView tv_cacheclear_scanning;private Button bt_cacheclear_clearall;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState); setContentView(R.layout.activity_cacheclear); pb_clearcache_scan = (ProgressBar) findViewById(R.id.pb_clearcache_scan); tv_cacheclear_scanning = (TextView) findViewById(R.id.tv_cacheclear_scanning); lv_clearcache_cachelist = (ListView) findViewById(R.id.lv_clearcache_cachelist); bt_cacheclear_clearall = (Button) findViewById(R.id.bt_cacheclear_clearall); getAllCache();}//获取当前APP缓存private void getAllCache() {// TODO Auto-generated method stub}}
0 0
- [安卓]手机管家(二十) 缓存清理
- [安卓]手机管家(十)正则表达式 & 号码归属地
- [安卓]手机管家(二)splash续(附加签名问题)
- [安卓]手机管家(十八)一键 锁屏清理线程以及widget
- 安卓开发之清理手机应用程序缓存
- [安卓]手机管家(一)splash
- [安卓]手机管家(三)homeActivity
- [安卓]手机管家(四)自定义属性
- [安卓]手机管家(十二)通讯卫士
- [安卓]手机管家(十六)进程管理
- [安卓]手机管家(二十一)杀毒UI及SlidingDrawer(抽屉效果)
- 安卓学习之(清理缓存)
- 安卓系统清理蓝牙缓存
- 安卓轻松实现清理缓存
- 立波锁屏管家:安卓手机锁屏变得简简单单
- [安卓]手机管家(五)防盗之加密
- [安卓]手机管家(七)防盗之左右划屏
- [安卓]手机管家(八)防盗之业务逻辑
- 深入理解javascript之this
- Ogre出现”…The minimum corner of the box must be less than or equal to maximum corner”错误
- easy=ui 树形结构案例
- 认识自己——恐惧的奴隶4:爸爸
- js刷新页面方法大全
- [安卓]手机管家(二十) 缓存清理
- Redis安装
- ActiveMQ(四):Topic方式使用MessageListener监听的方式接收消息
- 下载各种软件的地址 VMWARE
- TCP/IP参考模型的详解
- 黑马程序员——Objective-C语言知识整理——封装、继承与多态
- 根据英语谐音记单词
- WPF的WebBrowser屏蔽弹出脚本错误窗口
- Could not find any version that matches com.android.support:appcompat-v7:+