android 获取系统缓存之 java.lang.reflect.InvocationTargetException

android 获取系统缓存之 java.lang.reflect.InvocationTargetException

今天我给大家分享一个关于获取android手机应用缓存信息的方法以及androidO以上的版本适配问题。如果不做适配兼容的话就会报java.lang.reflect.InvocationTargetException错误。
以前我们在获取的方法如下:

public void getPackageSizeInfo(String packageName, IPackageStatsObserver.Stub observer) {
        Context context = App.getAppContext();
        try {
            PackageManager pm = App.sContext.getPackageManager();
            Method method = pm.getClass()
                    .getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
            method.invoke(pm, packageName, observer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class PackageStatsObserver extends IPackageStatsObserver.Stub {

        @Override
        public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
       			  Log.d("guojingbu", "packageName = "+pStats.packageName+"---"+(pStats.cacheSize + pStats.externalCacheSize));
        }
    }
   //使用
getPackageSizeInfo(“应用包名”,new  PackageStatsObserver ());

androidO以及以上如果再用上面的方法就会 报java.lang.reflect.InvocationTargetException错误。
androidO以上我们应该用下面的方法

@RequiresApi(api = Build.VERSION_CODES.O)
    private void getPackageSizeInfo(String packageName, IPackageStatsObserver.Stub observer) {
        Context context = App.getAppContext();
            final StorageStatsManager storageStatsManager = (StorageStatsManager) context.getSystemService(Context.STORAGE_STATS_SERVICE);
            try {
                ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
                StorageStats storageStats = storageStatsManager.queryStatsForUid(applicationInfo.storageUuid, applicationInfo.uid);
                long cacheSize = storageStats.getCacheBytes();
                long dataSize = storageStats.getDataBytes();
                long apkSize = storageStats.getAppBytes();
                Log.i("guojingbu", cacheSize + "---" + dataSize + "---" + apkSize);
                //long size += info.cacheSize;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

最后我们贴一下适配完整的方法:

private void getPackageSizeInfo(String packageName, IPackageStatsObserver.Stub observer) {
        Context context = App.getAppContext();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            final StorageStatsManager storageStatsManager = (StorageStatsManager) context.getSystemService(Context.STORAGE_STATS_SERVICE);
            final StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
            try {
                ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
                StorageStats storageStats = storageStatsManager.queryStatsForUid(applicationInfo.storageUuid, applicationInfo.uid);
                long cacheSize = storageStats.getCacheBytes();
                long dataSize = storageStats.getDataBytes();
                long apkSize = storageStats.getAppBytes();
                Log.i("guojingbu", cacheSize + ",," + dataSize + ",," + apkSize);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                PackageManager pm = App.sContext.getPackageManager();
                Method method = pm.getClass()
                        .getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
                method.invoke(pm, packageName, observer);
            }  catch (Exception e) {
                    Log.d("guojingbu", "error");
                    e.printStackTrace();
                }
            }
    }
    
 private class PackageStatsObserver extends IPackageStatsObserver.Stub {

        @Override
        public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
       			  Log.d("guojingbu", "packageName = "+pStats.packageName+"---"+(pStats.cacheSize + pStats.externalCacheSize));
        }
    }

今天的内容就分享完了。主要目的也是自己做一点笔记以便以后查阅。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值