我有使用IPackageStatsObserver计算已安装的应用程序存储大小,数据大小和缓存大小的方法,但在实际设备上没有触发callon.onSucces()。在模拟器上工作正常,但在从未调用的成功。我在清单中也有GET_PACKAGE_SIZE权限。
这是代码
public void requestAppInfo(String packageName, final AsyncDataReceiver<ArrayList<InstalledAppItem>> callback) {
PackageManager packageManager = getPackageManager();
try {
Method getPackageSizeInfo = packageManager.getClass().getMethod(GET_PACKAGE_SIZE_INFO, String.class, IPackageStatsObserver.class);
getPackageSizeInfo.invoke(packageManager, packageName, new IPackageStatsObserver.Stub() {
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) {
int appSize = (int) pStats.codeSize / 1048576;
int dataSize = (int) pStats.dataSize / 1024;
int cacheSize = (int) pStats.cacheSize / 1024;
systemData.clear();
systemData.add(new InstalledAppItem(0, "Internal Storage Used", appSize + " MB"));
systemData.add(new InstalledAppItem(1, "Data Used", dataSize + " KB"));
systemData.add(new InstalledAppItem(2, "Cache Used", cacheSize + "KB"));
//TODO Check Why callback not triggered on board.
callback.onSuccess(systemData);
}
});
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
答案 0 :(得分:1)
答案 1 :(得分:0)
已更新,用于支持Android O(API级别26)
从Android O(API级别26)开始,您不能使用getPackageSizeInfo
方法进行反思。以下帖子包括一个可以帮助您解决api级别26以上的应用程序的代码: