在项目中创建,android.content.pm 包名。里面创建两个aidl文件。PackageStats.aidl 和IPackageStatsObserver.aidl。

PackageStats.aidl

/* //device/java/android/android/view/WindowManager.aidl**** Copyright 2007, The Android Open Source Project**** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ****     http://www.apache.org/licenses/LICENSE-2.0 **** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License.*/package android.content.pm; parcelable PackageStats;

IPackageStatsObserver.aidl

/***** Copyright 2007, The Android Open Source Project**** Licensed under the Apache License, Version 2.0 (the "License");** you may not use this file except in compliance with the License.** You may obtain a copy of the License at****     http://www.apache.org/licenses/LICENSE-2.0**** Unless required by applicable law or agreed to in writing, software** distributed under the License is distributed on an "AS IS" BASIS,** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.** See 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 of cache directory, generate * statistics related to code, data, cache usage(TODO) * {@hide} */ oneway interface IPackageStatsObserver { void onGetStatsCompleted(in PackageStats pStats, boolean succeeded); }

 1 import android.app.Activity;  2 import android.content.pm.IPackageStatsObserver;  3 import android.content.pm.PackageManager;  4 import android.content.pm.PackageStats;  5 import android.os.Bundle;  6 import android.os.Handler;  7 import android.os.Message;  8 import android.widget.TextView;  9 10 public class MainActivity extends Activity { 11 12 private TextView tv; 13 private static final String ATTR_PACKAGE_STATS = "PackageStats"; 14 15  @Override 16 public void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 19 tv = new TextView(this); 20 21  setContentView(tv); 22 23 getpkginfo("com.xj.notebook"); 24  } 25 26 private Handler mHandler = new Handler() { 27 public void handleMessage(Message msg) { 28 switch (msg.what) { 29 case 1: 30 String infoString = ""; 31 PackageStats newPs = msg.getData().getParcelable( 32  ATTR_PACKAGE_STATS); 33 if (newPs != null) { 34 infoString += "应用程序大小: " + formatFileSize(newPs.codeSize); 35 infoString += "\n数据大小: " + formatFileSize(newPs.dataSize); 36 infoString += "\n缓存大小: " + formatFileSize(newPs.cacheSize); 37  } 38  tv.setText(infoString); 39 break; 40 default: 41 break; 42  } 43  } 44  }; 45 46 public void getpkginfo(String pkg) { 47 PackageManager pm = getPackageManager(); 48 try { 49 Method getPackageSizeInfo = pm.getClass().getMethod( 50 "getPackageSizeInfo", String.class, 51 IPackageStatsObserver.class); 52 getPackageSizeInfo.invoke(pm, pkg, new PkgSizeObserver()); 53 } catch (Exception e) { 54  } 55  } 56 57 class PkgSizeObserver extends IPackageStatsObserver.Stub { 58 public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) { 59 Message msg = mHandler.obtainMessage(1); 60 Bundle data = new Bundle(); 61  data.putParcelable(ATTR_PACKAGE_STATS, pStats); 62  msg.setData(data); 63  mHandler.sendMessage(msg); 65  } 66  } 67 68 /** 69  * 获取文件大小 70 */ 71 public static String formatFileSize(long length) { 72 String result = null; 73 int sub_string = 0; 74 if (length >= 1073741824) { 75 sub_string = String.valueOf((float) length / 1073741824).indexOf( 76 "."); 77 result = ((float) length / 1073741824 + "000").substring(0, 78 sub_string + 3) + "GB"; 79 } else if (length >= 1048576) { 80 sub_string = String.valueOf((float) length / 1048576).indexOf("."); 81 result = ((float) length / 1048576 + "000").substring(0, 82 sub_string + 3) + "MB"; 83 } else if (length >= 1024) { 84 sub_string = String.valueOf((float) length / 1024).indexOf("."); 85 result = ((float) length / 1024 + "000").substring(0, 86 sub_string + 3) + "KB"; 87 } else if (length < 1024) 88 result = Long.toString(length) + "B"; 89 return result; 90  } 91 }

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

更多相关文章

  1. 使用Android(安卓)Studio创建Android(安卓)Wear项目
  2. Android使用DialogFragment,自定义对话框
  3. android,view的执行过程onDraw、onSizeChanged,onFinishInflate
  4. [Android(安卓)Pro] 创建快捷方式,删除快捷方式,查询是否存在快捷
  5. SpannableString使用详解
  6. android 加载模式
  7. Android(安卓)ListView+image的使用
  8. Android应用程序创建桌面快捷方式
  9. Android(安卓)内存管理工具

随机推荐

  1. 数字九宫格作业
  2. PHP中的视图基类的实例演示
  3. css的太极图
  4. 宝塔安装Fastadmin访问后台报404错误
  5. 个人简历作业
  6. MYSQL主从配置
  7. CSS:盒模型-复习
  8. apollo 6.0 【开发版】从安装到启动,保姆
  9. 使用css制作太极图
  10. Mediapipe框架在Android上的使用