How Android Gets the app app size _android

Source: Internet
Author: User
Tags reflection

Android encapsulates this approach, and we don't have permission to call it, so we can just go through the aidl and use the Java reflection mechanism to invoke the system-level approach.

The following code: (note more detailed)

/** * Role:-----Get the size of the package-----* @param context * @param pkgname app's package name * @param appInfo entity class for storing some information about the app/PU Blic static void Getpkgsize (final context, String Pkgname, final phoneappinfo appInfo) {//Getpackagesizeinfo is P
  A private method in Ackagemanager, so it is necessary to invoke methods using the mechanism of reflection; try {method = PackageManager.class.getMethod ("Getpackagesizeinfo", New Class[]{string.class, Ipackagestatsobserve
   R.class}); Calling the Getpackagesizeinfo method requires two parameters: 1, the application package name to be detected, 2, the callback Method.invoke (Context.getpackagemanager (), Pkgname, new Ipacka Gestatsobserver.stub () {@Override public void ongetstatscompleted (Packagestats pstats, Boolean succeeded) thr
         oWS RemoteException {if (succeeded && pstats!= null) {synchronized (Phoneappinfo.class) { Appinfo.setcatchsize (pstats.cachesize);//Cache size appinfo.setdatasize (pstats.datasize); Data size appinfo.setcodesize (pstats.codesize); Apply Size appinfo.setappsize (PSTATS.CAchesize + pstats.codesize + pstats.datasize)//Total size of application log.d ("Asdasdxx", appinfo.getappsize () + "");
  }
       }
      }
     }); catch (Nosuchmethodexception | InvocationTargetException |
  Illegalaccessexception e) {e.printstacktrace (); }
 }

Here is the code for the two aidl files ...

Steps (Android Studio):

1, in the main folder, the establishment of a Aidl folder folder

2, the establishment of a package, the package named android.content.pm

3. Structure Drawing

Packagestats.aidl file ***************

* *//device/java/android/android/view/windowmanager.aidl
* *
* Copyright 2007, the Android Open Source Project
* *
* * * Licensed under the Apache License, Version 2.0 (the "License"); 
* * may not use this file except in compliance with the License. 
* * Obtain a copy of the License at * * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * * * *
Unless required by applicable or agreed into writing, software 
* * Distributed under the License is distributed O n an ' as is ' basis, 
* * without warranties or CONDITIONS of any KIND, either express OR implied. 
* * The License for the specific language governing permissions and * * Limitations under the License 
.
* *
package android.content.pm;
Parcelable Packagestats;

ipackagestatusobserver.aidl******************

* * * * * Copyright 2007, the Android Open Source Project * * * Licensed under the Apache License, Version 2.0 (the "Licen
Se ");
* * may not use this file except in compliance with the License. * * Obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * unless required by APPL Icable or agreed to in writing, software * * Distributed under the License is distributed on ' as is ' basis, * * Witho
UT warranties or CONDITIONS of any KIND, either express OR implied.
* * 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 package Manager. * Some usage scenarios include deletion cache directory, generate * Statistics related to code, data, cache usage (TODO * {@hide} */OneWay interface Ipackagestatsobserver {void ongetstatscompleted (in Packagestats pstats, Boolean succeeded); }

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.