码迷,mamicode.com
首页 > 移动开发 > 详细

(原)android4.2以后获取应用程序和缓存大小的方法(源码有改变)

时间:2015-02-08 15:28:05      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

以前获取应用的大小是用

 PackageManager mPackageManager= getPackageManager();

try {Method getPackageSizeInfoMethod = PackageManager.class.getDeclaredMethod("getPackageSizeInfo", String.class,    
              IPackageStatsObserver.class);  
getPackageSizeInfoMethod.invoke(mPackageManager, packageInfo.packageName,new MyObserver());  
} catch (Exception e) {  
} 

 

class MyObserver extends IPackageStatsObserver.Stub {  
  
    @Override  
    public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)  
            throws RemoteException {  
        long cacheSize = pStats.cacheSize;  
        String packageName = pStats.packageName;  
        String formatFileSize = Formatter.formatFileSize(  
                getApplicationContext(), cacheSize);  
        System.out.println(packageName + ":" + formatFileSize);  
    }  
}  

 

但在4.2后 PackageManager.getPackageSizeInfo(String packageName, IPackageStatsObserver observer)不可用,

改为PackageManager.getPackageSizeInfo(String packageName, int userHandle, IPackageStatsObserver observer),

至于增加的参数userHandle的解释是:The user whose size information should be retrieved。

这个userHandle怎么来? 通过系统类的UserHandle.myUserId()获取;

Method myUserId = UserHandle.class.getDeclaredMethod("myUserId");  
int userID = (Integer) myUserId.invoke(mPackageManager,null);  

 获取了以后就可以使用新的getPackageSizeInfo函数了。

getPackageSizeInfoMethod.invoke(mPackageManager,packageInfo.packageName, userID, new MyObserver() 
);

 

(原)android4.2以后获取应用程序和缓存大小的方法(源码有改变)

标签:

原文地址:http://www.cnblogs.com/liuying3013/p/4280054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!