Android System Cache related

Source: Internet
Author: User

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

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.