1. How to get the cache file and size for each app
Packagemanager pm =Context.getpackagemanager ();Try{Method Mmethod= Packagemanager.class. GetMethod ("Getpackagesizeinfo", String.class, Ipackagestatsobserver.class); List<PackageInfo> PackageInfos = pm.getinstalledpackages (0); //observer for data changes from Ipackagestatsobserver.stubDatachangedobserver Datachangedobserver =NewDatachangedobserver (); for(PackageInfo packageinfo:packageinfos) {//The system's method backs hide, so the reflection mechanism is used to get the methodMmethod.invoke (PM, Packageinfo.packagename, datachangedobserver); }} Catch(Exception e) {e.printstacktrace ();}
/*** The viewer of the scan result needs permission <uses-permission * android:name= "Android.permission.GET_PACKAGE_SIZE"/> * *@authorChenhao24 **/Private classDatachangedobserverextendsipackagestatsobserver.stub {@Override Public voidOngetstatscompleted (Packagestats pstats,BooleanSUCCEEDED)throwsRemoteException {if(!succeeded) { return; } //Get Cache Size LongCacheSize =pstats.cachesize; //get the name of the appString PackageName =Pstats.packagename; }}
Introduction of Ipackagestatsobserver.stub. Manually build android.content.pm package copy files into this directory
2. How do I empty the cache? You can use the similar method above to reflect the operation, but this operation requires system-level applications to clean up, how does the software on the market clean up? Skip to the App info page to clean it up manually, but the software on the market can clean up all the cache files, Using a mechanism of the Android system, when the system request space (much more than the current system idle size), if the system does not have enough space to use, the system will delete all the application cache files, so that the purpose of cleaning up all the cached files.
Packagemanager pm = Context.getpackagemanager (); Try { // Use reflection Method Mmethod = Packagemanager. Class. GetMethod ("Freestorageandnotify", Long. Class, Ipackagedataobserver. class ); Mmethod.invoke (PM, Integer.max_value,newcatch (Exception e) { E.printstacktrace ();}
/*** Application space, elimination of cached data viewer *@authorChenhao24 **/Private classApplydataobserverextendsipackagedataobserver.stub{@Override Public voidOnremovecompleted (String PackageName,BooleanSUCCEEDED)throwsRemoteException {if(!succeeded) {log.i (TAG,"Release SYS cache error"); return; } log.i (TAG,"Release SYS cache Success"); }}
Ipackagedataobserver.stub get similar to Ipackagestatsobserver.stub
Android System Cache related