How does Android get its own apk size?

Source: Internet
Author: User

Haven't updated the blog for a long time, recently saw the company's project to get the size of the app itself or from the server side back, still can't use. So xxx did not say. Here's how to get the size of your app.

Note: Here just get the size of your apk.

The first thing we need is permission:

<uses-permission android:name= "Android.permission.GET_PACKAGE_SIZE"/>

And then of course it's getting packagemanager:

Packagemanager Mpackagemanager=getpackagemanager (); try {PackageInfo pinfo=mpackagemanager.getpackageinfo ( Getpackagename (), 0); mpackagename=pinfo.packagename;} catch (Namenotfoundexception e) {e.printstacktrace ();}

In the next

Returns a list of all application packages installed on the device

List<applicationinfo> Installlist=mpackagemanager.getinstalledapplications (PackageManager.GET_UNINSTALLED _packages); for (int i = 0; i < installlist.size (); i++) {ApplicationInfo info=installlist.get (i); if ( Info.packageName.equals (Mpackagename)) {//If this packagename is the same as the packagename of the current APK, you can get the size of it. String dir=info.publicsourcedir;int size=integer.valueof ((int) new File (dir). Length ()); BigDecimal apksize=parseapksize (size); Tvsize.settext ("apk size:" +apksize+ "M");//The last displayed application size}}

Two decimal places are reserved in Java:

Private BigDecimal parseapksize (int size) {BigDecimal BD = new BigDecimal ((double) size/(1024*1024));                BigDecimal Setscale = Bd.setscale (2, Bigdecimal.round_down); return setscale;}


How does Android get its own apk size?

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.