Android一键清除某个应用下的数据
2014-07-04 17:32
218 查看
Android一键清除某个应用下的数据,我这里是针对android自带通讯录的数据清除,当初我用了几个方式来清理数据库,当初试用的数据估计有1000个联系人吧,当然跟自己存联系人的方式有关系,我这里用RawContacts表存联系人的基本信息,用data存联系人的详细信息,group是来存分类:
方式1:
把ContactsContract.CALLER_IS_SYNCADAPTER设置为true就能删除通讯录里的数据,
Uri uri = ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
getContentResolver().delete(uri,null,null);
this.getContentResolver().delete(Data.CONTENT_URI.buildUpon()
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER,"true").build(), null
, null);
getContentResolver().delete(ContactsContract.Data.CONTENT_URI,null,null);
uri = ContactsContract.Groups.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
getContentResolver().delete(uri,null,null);
花费时间在10秒左右,
方式2:
Cursor contactsCur = SdmLoginActivity.this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(contactsCur.moveToNext()){
//获取ID
String rawId = contactsCur.getString(contactsCur.getColumnIndex(ContactsContract.Contacts._ID));
//删除
String where = ContactsContract.Data._ID + " =?";
String[] whereparams = new String[]{rawId};
getContentResolver().delete(ContactsContract.RawContacts.CONTENT_URI, where, whereparams);
}
if (contactsCur!=null) {
contactsCur.close();
contactsCur = null;
}
耗费时间在20多秒左右,比上一种还慢.
方式3是在这里推荐的,在设置里有一个清除各个应用数据的地方,一般是设置->应用->选中自己的应用->清除数据就能清,该方式的耗费时间普遍在100毫秒左右,用7000多数据测花费时间也在100毫秒左右
设置里面用的方法是:
private void initiateClearUserData() {
mClearDataButton.setEnabled(false);
// Invoke uninstall or clear user data based on sysPackage
String packageName = mAppEntry.info.packageName;
Log.i(TAG, "Clearing user data for package : " + packageName);
if (mClearDataObserver == null) {
mClearDataObserver = new ClearUserDataObserver();
}
ActivityManager am = (ActivityManager)
getActivity().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);
showDialogInner(DLG_CANNOT_CLEAR_DATA, 0);
} else {
mClearDataButton.setText(R.string.recompute_size);
}
}
核心代码是:
ActivityManager am = (ActivityManager)
getActivity().getSystemService(Context.ACTIVITY_SERVICE);
am.clearApplicationUserData(packageName, mClearDataObserver);
而该方法是隐藏的方法只能通过反射方式调用
具体步骤:
1\在自己的应用上建一个包,包名:
android.content.pm
2\拷贝一下三个aidl到android.content.pm包下
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);
}
------------------------------------------------------------------------------------------------------
IPackageStatsObserver.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;
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);
}
---------------------------------------------------------------------------------------------------
PackageStats.addl文件:
/* //device/java/android/android/view/WindowManager.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;
parcelable PackageStats;
public void onRemoveCompleted(final String packageName, final boolean succeeded) {
// final Message msg = mHandler.obtainMessage(CLEAR_USER_DATA);
// msg.arg1 = succeeded?OP_SUCCESSFUL:OP_FAILED;
// mHandler.sendMessage(msg);
}
}
4/调用核心方法,我这里是通讯录,其系统自带通讯录包名:com.android.providers.contacts
用
Method method = am.getClass().getDeclaredMethod("clearApplicationUserData", IPackageDataObserver.class);
method.invoke(am, "com.android.providers.contacts", new ClearUserDataObserver());
或者:
ActivityManager am = (ActivityManager) SdmLoginActivity.this.getSystemService(Context.ACTIVITY_SERVICE);
Method methods[] = am.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
if ("clearApplicationUserData".equals(methods[i].getName())){
try {
methods[i].invoke(am, "com.android.providers.contacts", new ClearUserDataObserver());
break;
} catch (Exception e) {
}
}
}
<uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />
android:sharedUserId="android.uid.system"
7/编写Android.mk文件
方式1:
把ContactsContract.CALLER_IS_SYNCADAPTER设置为true就能删除通讯录里的数据,
Uri uri = ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
getContentResolver().delete(uri,null,null);
this.getContentResolver().delete(Data.CONTENT_URI.buildUpon()
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER,"true").build(), null
, null);
getContentResolver().delete(ContactsContract.Data.CONTENT_URI,null,null);
uri = ContactsContract.Groups.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
getContentResolver().delete(uri,null,null);
花费时间在10秒左右,
方式2:
Cursor contactsCur = SdmLoginActivity.this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(contactsCur.moveToNext()){
//获取ID
String rawId = contactsCur.getString(contactsCur.getColumnIndex(ContactsContract.Contacts._ID));
//删除
String where = ContactsContract.Data._ID + " =?";
String[] whereparams = new String[]{rawId};
getContentResolver().delete(ContactsContract.RawContacts.CONTENT_URI, where, whereparams);
}
if (contactsCur!=null) {
contactsCur.close();
contactsCur = null;
}
耗费时间在20多秒左右,比上一种还慢.
方式3是在这里推荐的,在设置里有一个清除各个应用数据的地方,一般是设置->应用->选中自己的应用->清除数据就能清,该方式的耗费时间普遍在100毫秒左右,用7000多数据测花费时间也在100毫秒左右
设置里面用的方法是:
private void initiateClearUserData() {
mClearDataButton.setEnabled(false);
// Invoke uninstall or clear user data based on sysPackage
String packageName = mAppEntry.info.packageName;
Log.i(TAG, "Clearing user data for package : " + packageName);
if (mClearDataObserver == null) {
mClearDataObserver = new ClearUserDataObserver();
}
ActivityManager am = (ActivityManager)
getActivity().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);
showDialogInner(DLG_CANNOT_CLEAR_DATA, 0);
} else {
mClearDataButton.setText(R.string.recompute_size);
}
}
核心代码是:
ActivityManager am = (ActivityManager)
getActivity().getSystemService(Context.ACTIVITY_SERVICE);
am.clearApplicationUserData(packageName, mClearDataObserver);
而该方法是隐藏的方法只能通过反射方式调用
具体步骤:
1\在自己的应用上建一个包,包名:
android.content.pm
2\拷贝一下三个aidl到android.content.pm包下
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);
}
------------------------------------------------------------------------------------------------------
IPackageStatsObserver.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;
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);
}
---------------------------------------------------------------------------------------------------
PackageStats.addl文件:
/* //device/java/android/android/view/WindowManager.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;
parcelable PackageStats;
3/IPackageStatsObserver这个类生成的桩
class ClearUserDataObserver extends IPackageDataObserver.Stub {public void onRemoveCompleted(final String packageName, final boolean succeeded) {
// final Message msg = mHandler.obtainMessage(CLEAR_USER_DATA);
// msg.arg1 = succeeded?OP_SUCCESSFUL:OP_FAILED;
// mHandler.sendMessage(msg);
}
}
4/调用核心方法,我这里是通讯录,其系统自带通讯录包名:com.android.providers.contacts
用
Method method = am.getClass().getDeclaredMethod("clearApplicationUserData", IPackageDataObserver.class);
method.invoke(am, "com.android.providers.contacts", new ClearUserDataObserver());
或者:
ActivityManager am = (ActivityManager) SdmLoginActivity.this.getSystemService(Context.ACTIVITY_SERVICE);
Method methods[] = am.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
if ("clearApplicationUserData".equals(methods[i].getName())){
try {
methods[i].invoke(am, "com.android.providers.contacts", new ClearUserDataObserver());
break;
} catch (Exception e) {
}
}
}
5/设置android.permission.CLEAR_APP_USER_DATA权限 google认为单单把clearApplicationUserData设置成@hide还不够安全,于是它要求执行clearApplicationUserData方法还需要应用具有android.permission.CLEAR_APP_USER_DATA权限。 在AndroidManifest.xml文件里面配置这个权限
<uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />
6/设置应用程序的系统进程权限
在AndroidManifest.xml的<manifest里面配置android:sharedUserId,如:android:sharedUserId="android.uid.system"
7/编写Android.mk文件
在Android.mk文件,加入LOCAL_CERTIFICATE := platform这一行, 调用系统平台的签名
相关文章推荐
- Android一键清除其他应用缓存数据,有没有什么好方法?
- android实现 彻底退出应用/清除数据/卸载应用的方法
- android 一键锁屏和清除手机数据
- android退出应用时候清除数据
- android 设置中清除数据 重启应用
- Android O :清除应用数据
- android 清除其他应用数据
- Android应用数据、缓存的清除和获取缓存大小
- android 在清除应用数据后,会重复生成shortcut
- Android应用自定义User Data管理(将Settings下的"清除数据"变成"管理空间")
- Android阶段学习笔记 8.4 之 一键锁屏 清除数据软件
- (原创)Android 清除第三方应用的数据缓存实现(包括清除系统应用缓存)
- 适用于android自动化测试中的命令行清除应用数据与缓存
- android一键清除最近应用按钮
- Android应用相关文件目录、清除数据和缓存的介绍
- 一键锁屏,激活设备管理器,卸载应用,清除数据
- Android一键锁屏和清除数据代码实现
- android 清除data/data/ 下其他应用的数据
- Android存储扩展学习-----应用的清除数据和清除缓存
- android 中清除应用的数据会清除哪些数据