Android Cleanup Cache feature implementation

Source: Internet
Author: User

We all know that you can view information about your application in the Android settings--and one of the functions is to clear the cache.


How to implement these functions, from the Android setting source code can get relevant information.

The implementation is as follows:

Java code:

Package Com.wang.clearcache;import Java.lang.reflect.method;import Android.os.bundle;import Android.os.remoteexception;import Android.app.activity;import Android.content.pm.ipackagestatsobserver;import Android.content.pm.packagemanager;import Android.content.pm.packagestats;public class MainActivity extends Activity {private packagemanager pm; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);p m = Getpackagemanager ();//Reflection try {Method method = PackageManager.class.getMethod ("Getpackagesizeinfo", New Class[]{string.class,ipackagestatsobserver.class}); Method.invoke (PM, new object[]{"Com.wang.clearcache", new Ipackagestatsobserver.stub () {@Overridepublic void Ongetstatscompleted (Packagestats pstats, Boolean succeeded) throws RemoteException {long cachesize = pstats.cachesize; Long codesize = Pstats.codesize;long datasize = pstats.datasize; System.out.println ("CacheSize:" + cachesize); System.out.println ("Codesize:"+ codesize); System.out.println ("datasize" + DataSize);}}); catch (Exception e) {e.printstacktrace ();}}}

Because the cache information needs to be added to the android.permission.GET_PACKAGE_SIZE permissions

Androidmainifest.xml

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.wang.clearcache "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission android:name=" android.perm Ission.        Get_package_size "/> <application android:allowbackup=" true "android:icon=" @drawable/ic_launcher " Android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:n                Ame= "com.wang.clearcache.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android . Intent.category.LAUNCHER "/> </intent-filter> </activity> </application></ma Nifest>
</pre><pre name= "code" class= "Java" >
Because using the Packagemanager getpackagesizeinfo function in code, this method is a non-public function, and all we need to do is call this function using a launch, In the method's internal callback ongetstatscompleted (Packagestats pstats, Boolean succeeded) this method, through the method of the Pstats parameters can be applied to the cache, data cache, code capacity cache, Need to use the system's Aidl file in the process

Ipackagestatsobserver:

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

Packagestats:

/*//device/java/android/android/view/windowmanager.aidl**** Copyright, the android Open Source project**** Licensed under the Apache License, Version 2.0 (the "License"); * * Do not use this file except in compliance with the License. * * Obtain a copy of the License at * * * * * * * * * *     unless required by app Licable law or agreed to in writing, software * * Distributed under the License is distributed on a "as is" BASIS, * * with Out 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;


Results of the last run:

Source Address Download:

http://download.csdn.net/detail/wangbiaohome/8026535

Android Cleanup Cache feature implementation

Related Article

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.