Android中应用程序清除data/data,清除cache,超详细

转自:https://www.cnblogs.com/rencm/p/5157114.html

清除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~~~~~~~~
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值