IPackageStatsObserver无法解析为类型(Android)
我想计算利用该设备的所有缓存的总和。为此,我收到了编译时间错误:
IPackageStatsObserver cannot be resolved to a type
我在代码中两次提到此错误:
public long totalCacheSize(){ totalSize=0;
PackageManager packageManager = getApplicationContext().getPackageManager();
/* List<PackageInfo> packs = packageManager
.getInstalledPackages(PackageManager.GET_ACTIVITIES);*/
// PackageManager.GET_META_DATA
List<PackageInfo> packs = packageManager
.getInstalledPackages(PackageManager.GET_META_DATA);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
Method getPackageSizeInfo;
try {
getPackageSizeInfo = packageManager.getClass()
.getMethod("getPackageSizeInfo",
String.class, Class.forName("android.content.pm.IPackageStatsObserver"));
getPackageSizeInfo.invoke(packageManager, p.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"+totalSize, Toast.LENGTH_SHORT).show();
}
}
);
} catch (Exception e) {
try {
getPackageSizeInfo = packageManager.getClass()
.getMethod("getPackageSizeInfo",
String.class, Class.forName("android.content.pm.IPackageStatsObserver"));
getPackageSizeInfo.invoke(packageManager, p.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"+totalSize, Toast.LENGTH_SHORT).show();
}
}
);
} catch (Exception ee) {
Log.d("eeeeeeeeeee", "error");
ee.printStackTrace();
}
}
}
Log.d("return size", totalSize+"");
Toast.makeText(getApplicationContext(), "return size"+totalSize, Toast.LENGTH_SHORT).show();
return totalSize;
}
IPackageStatsObserver
在android
SDK中不可用。也许使用加载Stub类Class.forName()
,找到默认的构造函数并调用它以获得a的新实例Stub
。但是我应该怎么编码,对此我一无所知!
救命 !!
回答:
要解决您的问题,请按照下列步骤操作:
- 右键单击项目
- 按
- 右键单击
aidl
文件夹 - 插入
android.content.pm
并按 - 下载IPackageStatsObserver.aidl
- 复制
android.content.pm
包内的帮助文件
- 右键单击
src
文件夹 - 插
android.content.pm
- 按
- 下载IPackageStatsObserver.aidl
- 复制
android.content.pm
包内的帮助文件 - 选择类别,
totalCacheSize
然后选择
以上是 IPackageStatsObserver无法解析为类型(Android) 的全部内容, 来源链接: utcz.com/qa/419609.html