(original) The method of obtaining the application and cache size after android4.2 (the source code has changed)

Source: Internet
Author: User

The size of the previously acquired app is used

Packagemanager mpackagemanager= Getpackagemanager ();

try {Method Getpackagesizeinfomethod = PackageManager.class.getDeclaredMethod ("Getpackagesizeinfo", String.class,                  ipackagestatsobserver.class);  Getpackagesizeinfomethod.invoke (Mpackagemanager, Packageinfo.packagename,new myobserver ());  } catch (Exception e) {  

Class Myobserver extends Ipackagestatsobserver.stub {        @Override public      void ongetstatscompleted (packagestats Pstats, Boolean succeeded)              throws RemoteException {          long cacheSize = pstats.cachesize;          String packagename = pstats.packagename;          String formatfilesize = formatter.formatfilesize (                  getapplicationcontext (), cacheSize);          System.out.println (PackageName + ":" + formatfilesize);      }  }  

But after 4.2 packagemanager.getpackagesizeinfo (String PackageName, Ipackagestatsobserver observer) is not available,

Change to Packagemanager.getpackagesizeinfo (String packagename, int userhandle, Ipackagestatsobserver observer),

As for the added parameter Userhandle explanation is: the user whose size information should be retrieved.

How does this userhandle come? Obtained through the Userhandle.myuserid () of the system class;

Method myUserID = UserHandle.class.getDeclaredMethod ("myUserID");  int UserID = (Integer) myuserid.invoke (mpackagemanager,null);  

You can use the new Getpackagesizeinfo function once you have obtained it.

Getpackagesizeinfomethod.invoke (Mpackagemanager,packageinfo.packagename, UserID, New Myobserver ());

(original) The method of obtaining the application and cache size after android4.2 (the source code has changed)

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.