首页 > 系统 > Android > 正文

Android获取app应用程序大小的方法

2019-10-24 20:34:45
字体:
来源:转载
供稿:网友

本文通过一段代码给大家介绍android获取app应用程序大小的方法,由于android对这种方法进行了封装,我们没有权限去调用这个方法,只能通过aidl,然后用java的反射机制去调用系统级方法,感兴趣的朋友一起学习吧

Android对这种方法进行了封装,我们没有权限去调用这个方法,所以我们只能通过AIDL,然后利用Java的反射机制去调用系统级的方法。

下面上代码:(注释比较详细)

 

 
  1. /** 
  2. * 作用:-----获取包的大小----- 
  3. * @param context 上下文 
  4. * @param pkgName app的包名 
  5. * @param appInfo 实体类,用于存放App的某些信息 
  6. */ 
  7. public static void getPkgSize(final Context context, String pkgName, final PhoneAppInfo appInfo) { 
  8. // getPackageSizeInfo是PackageManager中的一个private方法,所以需要通过反射的机制来调用 
  9. Method method; 
  10. try { 
  11. method = PackageManager.class.getMethod("getPackageSizeInfo"
  12. new Class[]{String.class, IPackageStatsObserver.class}); 
  13. // 调用 getPackageSizeInfo 方法,需要两个参数:1、需要检测的应用包名;2、回调 
  14. method.invoke(context.getPackageManager(), pkgName, 
  15. new IPackageStatsObserver.Stub() { 
  16. @Override 
  17. public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { 
  18. if (succeeded && pStats != null) { 
  19. synchronized (PhoneAppInfo.class) { 
  20. appInfo.setCatchSize(pStats.cacheSize);//缓存大小 
  21. appInfo.setDataSize(pStats.dataSize); //数据大小 
  22. appInfo.setCodeSize(pStats.codeSize); //应用大小 
  23. appInfo.setAppSize(pStats.cacheSize + pStats.codeSize + pStats.dataSize);//应用的总大小 
  24. Log.d("asdasdxx",appInfo.getAppSize()+""); 
  25. }); 
  26. catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { 
  27. e.printStackTrace(); 

下面是两个AIDL文件的代码。。。

步骤(Android Studio):

1、在main文件夹下,建立一个aidl文件夹的文件夹

2、建立一个包,包名为android.content.pm

3、结构图

Android获取app应用程序大小的方法

*******PackageStats.aidl文件***************

 

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

****************IPackageStatusObserver.aidl******************

 

 
  1. /* 
  2. ** 
  3. ** Copyright 2007, The Android Open Source Project 
  4. ** 
  5. ** Licensed under the Apache License, Version 2.0 (the "License"); 
  6. ** you may not use this file except in compliance with the License. 
  7. ** You may obtain a copy of the License at 
  8. ** 
  9. ** http://www.apache.org/licenses/LICENSE-2.0 
  10. ** 
  11. ** Unless required by applicable law or agreed to in writing, software 
  12. ** distributed under the License is distributed on an "AS IS" BASIS, 
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  14. ** See the License for the specific language governing permissions and 
  15. ** limitations under the License. 
  16. */ 
  17. package android.content.pm; 
  18. import android.content.pm.PackageStats; 
  19. /** 
  20. * API for package data change related callbacks from the Package Manager. 
  21. * Some usage scenarios include deletion of cache directory, generate 
  22. * statistics related to code, data, cache usage(TODO) 
  23. * {@hide} 
  24. */ 
  25. oneway interface IPackageStatsObserver { 
  26. void onGetStatsCompleted(in PackageStats pStats, boolean succeeded); 


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表