Android app clears Data/data, clears cache, super verbose

Source: Internet
Author: User

Clear the data, clear the cache, in fact, in the Android native setting has this function.

The requirement is to put this function into its own app and calculate the size of the cache and data.

So refer to the source of setting. See how this function is implemented:

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

Ipackagestatsobserver.aidl--Get the size of data and cache

/***** Copyright, the Android Open Source project**** Licensed under the Apache License, Version 2.0 (the "License"); * * If you are 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** Distribut  Ed under the License is distributed on a "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 Pac Kage 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 succ eeded);}

Ipackagedataobserver.aidl--Clear complete

/* * * * * Copyright, the Android Open Source Project * * * Licensed under the Apache License, Version 2.0 (the "Li  Cense ");  * * Do not use this file except in compliance with the License. * * 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 a "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 (TOD O) * {@hide} */OneWay interface Ipackagedataobserver {void onremovecompleted (in String packagename, Boolean su cceeded); }

  

The Java code then implements:

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

Purging 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,cache File size

private void GetSize (String packagename) {if (! Util.isnullstr (PackageName)) {Packagemanager Pmanager = Getpackagemanager ();p Manager.getpackagesizeinfo ( PackageName, Statsobserver);}} Ipackagestatsobserver statsobserver = new Ipackagestatsobserver.stub () {@Overridepublic void ongetstatscompleted ( Packagestats Pstats, Boolean succeeded) throws RemoteException {//TODO auto-generated method stubcatchesize = Pstats.data Size;handler.sendemptymessage (Constant.Hanler.MSG_GET_DATASIZE);}};

That's the ok~~~~~~~~.

Android app clears Data/data, clears cache, super verbose

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.