Android applications clear data/data, clear cache, ultra-detailed, androidcache

Source: Internet
Author: User

Android applications clear data/data, clear cache, ultra-detailed, androidcache

This function is available in Android native Setting to clear data and cache.

The requirement is to implement this function in your App and calculate the cache and data size.

So refer to the source code of Setting. See how to implement this function:

First, you need to write two aidl to call the system clearing and get the size function:

IPackageStatsObserver. aidl -- get the size of data and cache

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

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

  

Then implement the Java code:

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;}};};

Clear data by package name

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 {}}

Clear completion callback

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);           }        }    }

Get data and cache file 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);}};

So far, OK ~~~~~~~~

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.