java.lang.NoSuchMethodException: getPackageSizeInfo 刺骨的言语ヽ痛彻心扉 2022-07-12 10:07 61阅读 0赞 在手机卫士中,有缓存清理的功能,代码如下 private void getAppCacheSize(String packageName) { Class clazz = pm.getClass();// 获取PackageManager的类类型 try { Method method = clazz.getDeclaredMethod("getPackageSizeInfo", String.class,IPackageStatsObserver.class); // 把包名传递给回调对象 //getcacheInfo.packName = packageName; method.invoke(pm, packageName, new GetCacheInfo(packageName));// 结果回调在IPackageStatsObserver的对象中 } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } 报错如下 java.lang.reflect.InvocationTargetException java.lang.NoSuchMethodException: getPackageSizeInfo 在Android4.2以上版本中,PackageManager.getPackageSizeInfo(String packageName, IPackageStatsObserver observer)不可用,改为PackageManager.getPackageSizeInfo(String packageName, int userHandle, IPackageStatsObserver observer), 至于增加的参数userHandle的解释是:The user whose size information should be retrieved。下面是PackageManager内部的用法: /** * Retrieve the size information for a package. * Since this may take a little while, the result will * be posted back to the given observer. The calling context * should have the {@link android.Manifest.permission#GET_PACKAGE_SIZE} permission. * * @param packageName The name of the package whose size information is to be retrieved * @param userHandle The user whose size information should be retrieved. * @param observer An observer callback to get notified when the operation * is complete. * {@link android.content.pm.IPackageStatsObserver#onGetStatsCompleted(PackageStats, boolean)} * The observer's callback is invoked with a PackageStats object(containing the * code, data and cache sizes of the package) and a boolean value representing * the status of the operation. observer may be null to indicate that * no callback is desired. * * @hide */ public abstract void getPackageSizeInfo(String packageName, int userHandle, IPackageStatsObserver observer); /** * Like {@link #getPackageSizeInfo(String, int, IPackageStatsObserver)}, but * returns the size for the calling user. * * @hide */ public void getPackageSizeInfo(String packageName, IPackageStatsObserver observer) { getPackageSizeInfo(packageName, UserHandle.myUserId(), observer); } 代码修改为 private void getAppCacheSize(String packageName) { Class clazz = pm.getClass();// 获取PackageManager的类类型 try { Method method = clazz.getDeclaredMethod("getPackageSizeInfo", String.class, int.class,IPackageStatsObserver.class); /** * after invoking, PkgSizeObserver.onGetStatsCompleted() will be called as callback function. <br> * About the third parameter ‘Process.myUid() / 100000’,please check: * <android_source>/frameworks/base/core/java/android/content/pm/PackageManager.java: * getPackageSizeInfo(packageName, UserHandle.myUserId(), observer); */ // 把包名传递给回调对象 //getcacheInfo.packName = packageName; method.invoke(pm, packageName, android.os.Process.myUid() / 100000, new GetCacheInfo(packageName));// 结果回调在IPackageStatsObserver的对象中 } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } 运行后报错,添加相应的权限即可 java.lang.SecurityException: Neither user 10603 nor current process has android.permission.GET_PACKAGE_SIZE. java.lang.SecurityException: Neither user 10603 nor current process has android.permission.CLEAR_APP_CACHE. 测试后,可用。通过查阅资料,其中Process.myUid() / 100000的解释为: 表示多用户中的某一个用户,因为一个用户装的apk可以有100000个,用uid表示,uid/100000的商就是某个特定的用户id也叫userId或者userHandle。
相关 java.lang.NoSuchMethodException: getPackageSizeInfo 在手机卫士中,有缓存清理的功能,代码如下 private void getAppCacheSize(String packageName) { 刺骨的言语ヽ痛彻心扉/ 2022年07月12日 10:07/ 0 赞/ 62 阅读
还没有评论,来说两句吧...