Package Com.loaderman.appcachedemo;import Android.content.pm.ipackagedataobserver;import Android.content.pm.ipackagestatsobserver;import Android.content.pm.packagemanager;import Android.content.pm.packagestats;import Android.os.bundle;import Android.os.remoteexception;import Android.support.v7.app.appcompatactivity;import Android.text.format.formatter;import Android.view.View;import Android.widget.button;import Java.io.file;import Java.io.fileoutputstream;import Java.lang.reflect.Method;public Class Mainactivity extends Appcompatactivity {private Packagemanager MPM; Private Button Btncache; Private Button Btnclean; Private long cacheSize; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Analog cache Monicache (); Btncache = (Button) Findviewbyid (R.id.btn_cache); Btnclean = (Button) Findviewbyid (R.id.btn_clean); MPM = Getpackagemanager (); private void Monicache () {//Gets the current cache path: data/data/package name/cache File cachedir = Getcachedir (); Write something inside the cache. analog file Cachefile = new file (Cachedir, "cache.txt"); try {fileoutputstream out = new FileOutputStream (cachefile); Out.write ("Jfaklsdjfaklsdjfklasdjfkladsfjlkasdjflkasdflkasdjf". GetBytes ()); Out.flush (); Out.close (); } catch (Exception e) {e.printstacktrace (); }} public void Startscan (view view) {Saomiaocache (); private void Saomiaocache () {try {method = Mpm.getclass (). GetMethod ("Getpackagesizeinfo", String.class, Ipackagestatsobserver.class); Method.invoke (MPM, Getpackagename (), New Myobserver ()); } catch (Exception e) {e.printstacktrace (); }} public void Cleancache (view view) {try {method = Mpm.getclass (). GetMethod ("Freestorageandnotify", Long.class, Ipackagedataobserver.class); Method.invoke (MPM, Long.max_value, New Ipackagedataobserver.stub () {//Child threads @Override public void onremovecompleted (String PackageName, Boolean succeeded) throws RemoteException {Runonuithread () {new Runnable () {@Override public void run ( ) {Saomiaocache (); } }); } }); } catch (Exception e) {e.printstacktrace (); }} class Myobserver extends Ipackagestatsobserver.stub {//Run on child thread @Override public void Ongets Tatscompleted (Packagestats pstats, Boolean succeeded) throws RemoteException {//Cache size CacheSize = pstats.cachesize; System.out.println (cacheSize); Runonuithread (New RunnAble () {@Override public void run () {btncache.settext ("Cache Size:" + Formatt Er.formatfilesize (Getapplicationcontext (), cacheSize)); } }); } }}
Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android " xmlns:tools=" Http://schemas.android.com/tools " android:id=" @+id/activity_main " Android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical " tools:context=" com.loaderman.appcachedemo.MainActivity "> <button android:id=" @+id/btn_ Cache " android:onclick=" Startscan " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android:text=" Scan cache "/> <button android:id=" @+id/btn_clean " android:o nclick= "Cleancache" android:layout_width= "wrap_content" android:layout_height= "Wrap_content " android:text= "Clean cache"/></linearlayout>
Add Permissions:
<uses-permission android:name= "Android.permission.CLEAR_APP_CACHE"/> <uses-permission android:name= "Android.permission.GET_PACKAGE_SIZE"/>
Create a aidl file below main
New package name is: android.content.pm
Ipackagedataobserver.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.**-may obtain A copy of the License at**** http://www.apache.org/licenses/license-2.0**** unless required by applicable law or Agre Ed to in writing, software** distributed under the License are distributed on a "as is" basis,** without warranties OR CON Ditions of any KIND, either express or implied.** see the License for the specific language governing permissions and** Li Mitations under the license.*/package android.content.pm;/** * API for package data change related callbacks from the Pack Age 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);}
ipackagestatsobserver.aidl
/***** 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.aidl
/*//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;
:
To skip to the System app information page to clean up the cache:
Jump to System Application Information page Intent infointent = new Intent (); Infointent.setaction (settings.action_application_details_settings); Uri uri = uri.fromparts ("Package", Getpackagename (), null),//uri.parse //("Package:" + mcurrentinfo.packagename); Infointent.setdata (URI); StartActivity (infointent);
The realization of the access and cleanup function of mobile app cache