How to obtain the size of installed apps in Android

Source: Internet
Author: User

How can I obtain the size of the installed application at the application level?
There are two ways to search online:
1. directly obtain the corresponding package under the data Directory and use the File. length () method. Then we will find that the size is different from that shown in the settings.
2. Obtain the PackageStats class instance by calling the source code method in the settings through reflection.
You don't want to use either of the two methods...
Then I found the solution:
The PackageManager class has a getPackageSizeInfo method as follows: Copy codeThe Code is as follows: public abstract void getPackageSizeInfo (String packageName,
IPackageStatsObserver observer );

But this method is hide.
Compile the android2.3 Framework by yourself and copy the jar package. You only need to import the Framework to the library, you can use your own compiled framework over the default android lib. In fact, it allows your application layer to call interfaces in the framework.
Download framework: click here
After the download, add the classes. jar to the lib by adding the lib,






Then, pack your lib to Android2.3.3 provided by the sdk. Note that you cannot directly use Add External JARs for direct import.




The following describes how to use it directly.
Copy codeThe Code is as follows: pm. getPackageSizeInfo (packageInfo. packageName,
New IPackageStatsObserver. Stub (){
Public void onGetStatsCompleted (PackageStats stats,
Boolean succeeded ){
Log. v ("King", stats. codeSize );
Log. v ("King", stats. dataSize );
Log. v ("King", stats. cacheSize );
}
});

The first parameter is the package name, which can be obtained by everyone.
The second parameter is the anonymous internal class that implements the JAVA interface generated by AIDL. The program size data can be processed in the callback method.
Stats. codeSize: application size
Stats. dataSize: data size
Stats. cacheSize: cache size
Generally, you may use listView to display the size of each application. Calling the getPackageSizeInfo method cannot obtain the value immediately, so it must be asynchronously displayed with other data.

You can use handler to send the data and then call the notifyDataSetChanged () method of the Adapter to update and display the application size.
You may also want to click on each item to directly jump to the application management interface in the settings, instead of writing uninstallation, clearing the cache, and other methods. You can use the following method to set source code:Copy codeThe Code is as follows: Intent intent = new Intent (Settings. ACTION_APPLICATION_DETAILS_SETTINGS, Uri. fromParts ("package", package name, null ));
// Start new activity to display extended information
StartActivityForResult (intent, 1 );

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.