admin 管理员组

文章数量: 1184232

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
**
**

**
** 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)
*
}
*/
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
**
**

**
** 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)
*
}
*/
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
**
**

**
** 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这一行, 调用系统平台的签名

本文标签: 权限 方式 文件