Referce to:http://www.baidufe.com/item/8786bc2e95a042320bef.html
Calculates the size of the mobile phone memory (RAM) used by the Android app, the size of the data that the app generates, and the amount of disk space (ROM) the app uses. Of course, this must be used to Packagemanager.
1, check the Android Packagemanager source code, find the Getpackagesizeinfo method:
/*** Retrieve The size information for a package. * Since This is a little while, the result would * is posted back To the given observer. The calling context * should has the {@linkAndroid. Manifest.permission#get_package_size} permission. * * @paramPackageName the name of the package whose size information are to be retrieved *@paramObserver an observer callback to get notified when the operation * are complete. * {@linkandroid.content.pm.ipackagestatsobserver#ongetstatscompleted (Packagestats, Boolean)} * The Observer ' s callback is invoked with a Packagestats object (containing the * code, data and cache sizes of the package) and a Boolean value repr Esenting * The status of the operation. Observer may null to indicate that * No callback is desired. * * @hide*/ Public Abstract voidGetpackagesizeinfo (String PackageName, Ipackagestatsobserver observer);
2. The Getpackagesizeinfo method has two parameters, the first one is the app package name that needs to be calculated, and the second is a callback. But ipackagestatesobserver this class in the API seemingly can't find, find a bit of information, need to through Android Aidl way to do. Method:
1), new ANDROID.CONTENT.PM package in SRC directory
2), under the package to create a new Packagestats.aidl file, the contents are as follows:
Package android.content.pm; Parcelable packagestats;
3), in the package under the new Ipackagestatsobserver.aidl interface file, the contents are as follows:
package android.content.pm; import Android.content.pm.PackageStats; /** * API for package data change related callbacks from the package Manager. * Some Usage Scen Arios include deletion of cache directory, generate * Statistics related to code, data, Cache usage (TODO) * {@hide} */ Interface Ipackagestatsobserver { void< /span> ongetstatscompleted (in Packagestats pstats, boolean succeeded);}
3. The Getpackagesizeinfo method cannot be called by Context.getPackageManager.getPackageSizeInfo, because it is actually an invoke-constrained method, so it must be implemented by reflection:
/*** Get the Android Native app cache size, data size, application size * *@paramContext * Context Object *@parampkgname * Native App package name to be detected *@throwsnosuchmethodexception *@throwsInvocationTargetException *@throwsillegalaccessexception*/ Public Static voidGetpkgsize (FinalContext context, String pkgname)throwsnosuchmethodexception, InvocationTargetException, illegalaccessexception {//Getpackagesizeinfo is a private method in Packagemanager, so a reflection mechanism is required to invoke themethod = Packagemanager.class. GetMethod ("Getpackagesizeinfo", NewClass[] {String.class, Ipackagestatsobserver.class }); //calling the Getpackagesizeinfo method requires two parameters: 1, the application package name to be detected; 2, callbackMethod.invoke (Context.getpackagemanager (),Newobject[] {pkgname,Newipackagestatsobserver.stub () {@Override Public voidOngetstatscompleted (Packagestats pstats,BooleanSUCCEEDED)throwsRemoteException {//The message loop cannot be processed by default in a child thread, and the toast cannot be displayed naturally, so a manual looper is required .Looper.prepare (); //extract all the required data from the PstatsToast.maketext (Context,"Cache size =" + formatter.formatfilesize (context, pstats.cachesize) + "\ n Data size =" + Formatter.formatfil Esize (context, pstats.datasize) + "\ n Program size =" +formatter.formatfilesize (context, pstats.codesize), Toast.length_long). Show (); //traverse a message queue, pop toastLooper.loop (); } } });}
I directly display it directly in the observer callback by toast, but this callback is done asynchronously in the child thread, and the message loop is not processed by default in the child thread. So toast.show can not be executed correctly, you need to manually add the content of the toast display, add to the Looper, respectively, call Looper.prepare and Looper.loop Method!
4, of course, according to Packagemanager in the Getpackagesizeinfo note, you also need to add permission in Androidmanifest.xml:
<android:name= "Android.permission.GET_PACKAGE_SIZE"></ Uses-permission>
[Android Pro] Packagemanager#getpackagesizeinfo (Hide)