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?