gpt4 book ai didi

Android,method.invoke 在 SDK 26,Oreo 中崩溃?

转载 作者:行者123 更新时间:2023-11-29 02:30:54 24 4
gpt4 key购买 nike

这是我在 StackOverflow 上的第一篇文章。

我在我的增强应用程序中使用了反射,如下所示。它在 sdk<26 中运行良好,但在 sdk 26 中我得到了 java.lang.reflect.InvocationTargetException 异常。我怎样才能让它在 android 8 中恢复工作?

我的代码是这样的:

Method pkgMethod = getPackageManager().getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
pkgMethod.invoke(getPackageManager(), pkg.packageName, new IPackageStatsObserver.Stub() {
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
long cacheSize = pStats.externalCacheSize;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
cacheSize += pStats.cacheSize;
if (cacheSize > 0) {
Drawable icon = null;
try {
icon = getPackageManager().getApplicationIcon(pkg.packageName);
} catch (PackageManager.NameNotFoundException ignored) {
}
String name = pkg.packageName;
try {
PackageManager pm = getApplicationContext().getPackageManager();
name = (String) pm.getApplicationLabel(pm.getApplicationInfo(pkg.packageName, PackageManager.GET_META_DATA));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
totalSize += cacheSize;
appList.add(new Items(icon, name, pkg.packageName, cacheSize, true, ""));

publishProgress();
}
countDownLatch.countDown();
}
});

这是我得到的异常:

java.lang.reflect.InvocationTargetException
04-05 05:20:14.027 2701-4724/com.mydidantivirus.android W/System.err:
at java.lang.reflect.Method.invoke(Native Method)
04-05 05:20:14.027 2701-4724/com.myantivirus.android W/System.err:
at com.my.antivirus.ui.JunkFileActivity$ScanTask.doInBackground(JunkFileActivity.java:285)
04-05 05:20:14.028 2701-4724/com.myantivirus.android W/System.err: at com.my.antivirus.ui.JunkFileActivity$ScanTask.doInBackground(JunkFileActivity.java:260)
04-05 05:20:14.028 2701-4724/com.myantivirus.android W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
04-05 05:20:14.029 2701-4724/com.myantivirus.android W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
04-05 05:20:14.029 2701-4724/com.myantivirus.android W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
04-05 05:20:14.029 2701-4724/com.myantivirus.android W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
04-05 05:20:14.029 2701-4724/com.myantivirus.android W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
04-05 05:20:14.029 2701-4724/com.myantivirus.android W/System.err: at java.lang.Thread.run(Thread.java:764)
04-05 05:20:14.030 2701-4724/com.myantivirus.android W/System.err: Caused by: java.lang.NullPointerException: Attempt to read from field 'long android.content.pm.PackageStats.externalCacheSize' on a null object reference
04-05 05:20:14.030 2701-4724/com.myantivirus.android W/System.err: at com.my.antivirus.ui.JunkFileActivity$ScanTask$1.onGetStatsCompleted(JunkFileActivity.java:288)
04-05 05:20:14.030 2701-4724/com.myantivirus.android W/System.err: at android.app.ApplicationPackageManager.getPackageSizeInfoAsUser(ApplicationPackageManager.java:2167)
04-05 05:20:14.030 2701-4724/com.myantivirus.android W/System.err: at android.content.pm.PackageManager.getPackageSizeInfo(PackageManager.java:5065)
04-05 05:20:14.030 2701-4724/com.myantivirus.android W/System.err: ... 9 more

我还在 Android Oreo 上尝试了一个缓存清理器库并得到了这个异常:

java.lang.reflect.InvocationTargetException
04-05 06:50:46.590 8639-8741/org.mazhuang.cleanexpert W/System.err: at
java.lang.reflect.Method.invoke(Native Method)
04-05 06:50:46.590 8639-8741/org.mazhuang.cleanexpert W/System.err: at org.mazhuang.cleanexpert.util.CleanUtil.freeAllAppsCache(CleanUtil.java:96)
04-05 06:50:46.591 8639-8741/org.mazhuang.cleanexpert W/System.err: at org.mazhuang.cleanexpert.ui.JunkCleanActivity$5.run(JunkCleanActivity.java:306)
04-05 06:50:46.591 8639-8741/org.mazhuang.cleanexpert W/System.err: at java.lang.Thread.run(Thread.java:764)
04-05 06:50:46.591 8639-8741/org.mazhuang.cleanexpert W/System.err: Caused by: java.lang.SecurityException: Neither user 10087 nor current process has android.permission.CLEAR_APP_CACHE.
04-05 06:50:46.591 8639-8741/org.mazhuang.cleanexpert W/System.err: at android.os.Parcel.readException(Parcel.java:1942)
04-05 06:50:46.591 8639-8741/org.mazhuang.cleanexpert W/System.err: at android.os.Parcel.readException(Parcel.java:1888)
04-05 06:50:46.591 8639-8741/org.mazhuang.cleanexpert W/System.err: at android.content.pm.IPackageManager$Stub$Proxy.freeStorageAndNotify(IPackageManager.java:4865)
04-05 06:50:46.591 8639-8741/org.mazhuang.cleanexpert W/System.err: at android.app.ApplicationPackageManager.freeStorageAndNotify(ApplicationPackageManager.java:2112)
04-05 06:50:46.592 8639-8741/org.mazhuang.cleanexpert W/System.err: at android.content.pm.PackageManager.freeStorageAndNotify(PackageManager.java:4996)
04-05 06:50:46.593 8639-8741/org.mazhuang.cleanexpert W/System.err: ... 4 more

最佳答案

已更新

从 Android O(API 级别 26)开始,您不能使用带反射的 getPackageSizeInfo 方法。以下是一个代码,可以帮助您在 api 级别 26 以下和以上:

private void getPackageSizeInfo(final Context context, String packageName) {
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 ai = context.getPackageManager().getApplicationInfo(packageName, 0);
StorageStats storageStats = storageStatsManager.queryStatsForUid(ai.storageUuid, getApplicationInfo().uid);
long cacheSize =storageStats.getCacheBytes();
long dataSize =storageStats.getDataBytes();
long apkSize =storageStats.getAppBytes();
Toast.makeText(context, cacheSize + ",," + dataSize + ",," + apkSize, Toast.LENGTH_LONG).show();
//long size += info.cacheSize;
} catch (Exception e) {
e.printStackTrace();
}
} else {
Method getPackageSizeInfo;
try {
getPackageSizeInfo = context.getPackageManager().getClass()
.getMethod("getPackageSizeInfo",
String.class, Class.forName("android.content.pm.IPackageStatsObserver"));

getPackageSizeInfo.invoke(context.getPackageManager(), packageName,
new IPackageStatsObserver.Stub() { //error

public void onGetStatsCompleted(
PackageStats pStats, boolean succeeded)
throws RemoteException {

//totalSize = totalSize + pStats.cacheSize;
//Log.d("size", totalSize+"");
Toast.makeText(getApplicationContext(), "size"+pStats.cacheSize + ",," + pStats.dataSize + ",," + pStats.codeSize, Toast.LENGTH_SHORT).show();
}
}
);
} catch (Exception e) {
try {
getPackageSizeInfo = context.getPackageManager().getClass()
.getMethod("getPackageSizeInfo",
String.class, Class.forName("android.content.pm.IPackageStatsObserver"));

getPackageSizeInfo.invoke(context.getPackageManager(), packageName,
new IPackageStatsObserver.Stub() { //error

public void onGetStatsCompleted(
PackageStats pStats, boolean succeeded)
throws RemoteException {
Toast.makeText(getApplicationContext(), "size"+pStats.cacheSize, Toast.LENGTH_SHORT).show();
}
}
);
} catch (Exception ee) {
Log.d("eeeeeeeeeee", "error");
ee.printStackTrace();
}
}
}
}

请注意,为了使代码正常工作,API 级别低于 26,您应该根据以下要求配置 AIDL 和要求:https://stackoverflow.com/a/30278018/1939409

关于Android,method.invoke 在 SDK 26,Oreo 中崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49667101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com