Android中应用程序清除data/data,清除cache,超详细
清除data,清除cache,其实在Android原生Setting里面有这个功能的。
需求是把这个功能做到自己的App里面,并计算出cache和data的size。
所以参考了一下Setting的源码。看如何实现该功能,该功能是需要在源码下编译的
首先需要写两个aidl去调用系统的清除以及获取size功能:
IPackageStatsObserver.aidl -- 获取data以及cache的size
/*
**
** 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);
}
IPackageDataObserver.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; /**
* 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 IPackageDataObserver {
void onRemoveCompleted(in String packageName, boolean succeeded);
}
然后Java代码实现:
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case Constant.Hanler.MSG_GET_DATASIZE:
String size=Formatter.formatFileSize(AppDetialActivity.this, catcheSize);
tv_catcheSize.setText(size);
break;
case Constant.Hanler.MSG_CLEAR_DATA_SUCCESS:
String pkgname=(String) msg.obj;
getSize(pkgname);
break;
default:
break;
}
};
};
通过包名清除数据
private ClearUserDataObserver mClearDataObserver;
private void clearData(String packagename){
if (mClearDataObserver == null) {
mClearDataObserver = new ClearUserDataObserver();
}
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
boolean res = am.clearApplicationUserData(packageName,
mClearDataObserver);
if (!res) {
// Clearing data failed for some obscure reason. Just log error for now
Log.i(TAG, "Couldnt clear application user data for package:"
+ packageName);
showToast("Clear failed");
} else { }
}
清除完成回调
class ClearUserDataObserver extends IPackageDataObserver.Stub {
public void onRemoveCompleted(final String packageName, final boolean succeeded) {
Logger.d(TAG, "packageName "+packageName +" succeeded "+succeeded);
if(succeeded){
Message msg=Message.obtain();
msg.what=Constant.Hanler.MSG_CLEAR_DATA_SUCCESS;
msg.obj=packageName;
handler.sendMessage(msg);
}
}
}
获取data,cache文件size
private void getSize(String packageName) {
if (!Util.isNullStr(packageName)) {
PackageManager pManager = getPackageManager();
pManager.getPackageSizeInfo(packageName, statsObserver);
}
}
IPackageStatsObserver statsObserver = new IPackageStatsObserver.Stub() {
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
throws RemoteException {
// TODO Auto-generated method stub
catcheSize = pStats.dataSize;
handler.sendEmptyMessage(Constant.Hanler.MSG_GET_DATASIZE);
}
};
到此就OK~~~~~~~~
本文为博主原创文章,转载请注明出处
http://www.cnblogs.com/rencm/p/5157114.html
Android中应用程序清除data/data,清除cache,超详细的更多相关文章
- Android中应用程序如何获得系统签名权限
有些库的使用条件比较苛刻,要求同一签名的程序才可以获得访问权.此时即便是在AndroidManifest.xml中添加了相应的permission,依旧会得到没有xx访问权限的问题.比如android ...
- Android中获取正在运行的应用程序-----ActivityManager.RunningAppProcessInfo类详解
今天继续讲解关于ActivityManager的使用,通过前面一节的学习,我们学会了如何利用ActivityManager获取系统里 正在运行的进程.本文要讲解的知识点是利用这些进程信息获取系统里正在 ...
- Android中的sharedUserId属性详解
在Android里面每个app都有一个唯一的linux user ID,则这样权限就被设置成该应用程序的文件只对该用户可见,只对该应用程序自身可见,而我们可以使他们对其他的应用程序可见,这会使我们用到 ...
- Android中@id与@+id区别
Android中的组件需要用一个int类型的值来表示,这个值也就是组件标签中的id属性值. id属性只能接受资源类型的值,也就是必须以@开头的值,例如,@id/abc.@+id/xyz等. 如果在@后 ...
- Android中@id与@+id区别和sharedUserId属性详解
Android中的组件需要用一个int类型的值来表示,这个值也就是组件标签中的id属性值. id属性只能接受资源类型的值,也就是必须以@开头的值,例如,@id/abc.@+id/xyz等. 如果在@后 ...
- Android中后台的劳动者“服务”
前言 作为四大组件之一的Service,想必不少开发者都是了解的,那具体熟悉吗?是不是对Service中的每个知识点是否了解,它与Activity的关系又是什么样的,我们所理解的后台服务跟Servic ...
- Android中的Context(一)
Android中的Context(一) 在Android开发中,Context可以说是我们接触地非常多的一个概念了,也译作"上下文",但是这个上下文到底是什么却并不好理解. 通俗的 ...
- Android中的广播Broadcast详解
今天来看一下Android中的广播机制,我们知道广播Broadcast是Android中的四大组件之一,可见他的重要性了,当然它的用途也很大的,比如一些系统的广播:电量低.开机.锁屏等一些操作都会发送 ...
- 暴力清除Android中的短信
有些短信程序有bug,当短信(特别是彩信)没有接收完整,或者是一些异常情况下,你会收到一条短信但是看不到或者看不了. 此时郁闷的事情就来了,系统会提醒你还有1条未读短信,但是你满世界都找不到这条短信. ...
随机推荐
- SQL--类型转换
Convert函数和Cast函数都是转化函数,效果是一样的. cast函数,转化,如果转化之后,年龄还是Null的话,就显示为“未知” Convert函数和Cast函数都是转化函数,效果是一样的.
- C#实现网页爬虫
HTTP请求工具类(功能:1.获取网页html:2.下载网络图片:): using System; using System.Collections.Generic; using System.Dra ...
- Razor练习1
学习ASP.NET MVC, Razor语法必须掌握,这篇学习: Razor code blocks are enclosed in @{ ... }Inline expressions (varia ...
- 【VBS】vbs指定编码保存文本文件(含xml、ini什么的)
本文还是折腾安装包期间衍生出来的产物. 我那安装包在安装期间有这个动作: - 让用户填写一些信息,待安装完成后把这些信息写入软件安装目录中的指定ini.xml文件中 上文说的是如何用vbs写ini,i ...
- 关于 Servlet 和 Web
文中也只是对Servlet和Web作简单的了解,有个初步的认识,深入的内容有待于进一步去研究. T. T _ . _ Servlet Servlet(Server Applet),全称J ...
- MEF入门之不求甚解,但力求简单能讲明白(二)
在上一篇文章中,我们已经学到了很基本的MEF概念和使用方法. 但我们导出的是一个object类型的实例,只能用来tostring,没有引用部件类库,也不能用里面的成员方法. 本篇,我们逐渐往简单的文件 ...
- VS2010如何使用Visual Studio Online在线服务管理团队资源(在线TFS)
前言 Visual Studio Online,也就是以前的Team Foundation Service,从名字可以看出这是一个团队资源管理服务.在微软的云基础架构中运行,无需安装或配置任何服务器, ...
- 关于Bugzilla WebService接口
参考:http://www.bugzilla.org/docs/3.2/en/html/api/Bugzilla/WebService.html http://www.bugzilla.org/doc ...
- IOS高德地图逆地理编码定位+网络判断
先说下这功能的流程, 流程:判断用户是否联网--->获取用户地理位置经纬度--->通过经纬度去查询地理位置名称 //高德地图 @property (nonatomic, strong) ...
- 「轉」Java的内存机制
0.参考资料: http://www.j2megame.org/index.php/content/view/2246/125.html 1.Java的内存机制 Java 把内存划分成两种:一种是栈内 ...