Android中應用程式清除data/data,清除cache,超詳細,androidcache

來源:互聯網
上載者:User

Android中應用程式清除data/data,清除cache,超詳細,androidcache

清除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 nowLog.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() {@Overridepublic void onGetStatsCompleted(PackageStats pStats, boolean succeeded)throws RemoteException {// TODO Auto-generated method stubcatcheSize = pStats.dataSize;handler.sendEmptyMessage(Constant.Hanler.MSG_GET_DATASIZE);}};

到此就OK~~~~~~~~  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.