PackageManager.getPackageSizeInfo||UserHandle.myUserId()
跟着教程去写获得缓存的逻辑。但是死活不出现。查了一下log说我参数不对。
然后看了看源码。和教程的不一样。多了一个参数。UserHandle.myUserId()。查了一下,发写法
新写法:
Method myUserId = UserHandle.class.getDeclaredMethod("myUserId"); //先获得这个userid(我也不太清楚是干嘛的,稍后我查查)
int userID = (Integer) myUserId.invoke(pm,null); //pm是packagemanager。自己getpackagemanager就好了
getPackageSizeInfoMethod.invoke(pm,info.packageName,userID,new MyDataObserver()); //info.packagename是包名 mydataobserver是实现类。自己完成回调函数
//回调函数参考
private class MyDataObserver extends IPackageStatsObserver.Stub {
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
throws RemoteException {
final long cache = pStats.cacheSize;
long code = pStats.codeSize;
long data = pStats.dataSize;
String packageName = pStats.packageName;
final ApplicationInfo appinfo;
try {
appinfo = pm.getApplicationInfo(packageName, 0);
runOnUiThread(new Runnable() {
public void run() {
tv_scan_status.setText("正在扫描:" + appinfo.loadLabel(pm));
if (cache > 0) {
TextView tv = new TextView(getApplicationContext());
tv.setText(appinfo.loadLabel(pm)
+ "-缓存大小:"
+ Formatter.formatFileSize(
getApplicationContext(), cache));
tv.setTextColor(Color.BLACK);
ll_container.addView(tv, 0);
}
}
});
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}