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.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd2fuz2jpyw9ob21l/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
How to implement these functions, from the Android setting source code can get relevant information.
Implementations such as the following:
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 ();}}}
Increased Android.permission.GET_PACKAGE_SIZE permissions due to cached information
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 of the use of Packagemanager in code, the Getpackagesizeinfo function is used. But this method is incorrect outside the public function, all we need to use the launch to call this function, in the method's internal callback ongetstatscompleted (Packagestats pstats, Boolean succeeded) this method, The pstats parameter of this method can get the application cache, the data cache, the code capacity cache, the system's Aidl file should be used 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;
The result of the final execution:
Source code address Download:
http://download.csdn.net/detail/wangbiaohome/8026535
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Android Clear cache feature to implement