我正在尝试清除除我自己之外的其他 android 应用程序的应用程序缓存。为此,我在 PackageManager 类上使用反射。但是,每当我在调用它之前初始化该方法时,它总是最终为空。
private void initiateClearUserData() {
// Invoke uninstall or clear user data based on sysPackage
String thePackageName;
PackageManager pm = speedy.this.getPackageManager();
List<ApplicationInfo> installedApps = pm.getInstalledApplications(0);
ApplicationInfo ai;// = installedApps.get(0);
ActivityManager.RunningAppProcessInfo process;
for(int x=0; x<4; x++){
ai = installedApps.get(x);
这是我的问题所在:
thePackageName = ai.packageName.toString();// mAppEntry.info.packageName;
Method deleteApplicationCacheFiles = null;
mClearCacheObserver = new ClearCacheObserver();
try {
deleteApplicationCacheFiles = pm.getClass().getMethod(
"deleteApplicationCacheFiles", String.class, PackageManager.class);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(deleteApplicationCacheFiles!= null){
try {
deleteApplicationCacheFiles.invoke(thePackageName, mClearCacheObserver);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Toast.makeText(speedy.this, "Hell naw",
Toast.LENGTH_SHORT).show();
}
}
}
因为方法 deleteApplicationCacheFiles 为空,所以我的 toast 消息出现了。有什么建议么?