私はアンドロイドに非常に慣れていません。1 つのボタンをクリックすると、Android デバイスにインストールされているすべてのアプリケーションのキャッシュ メモリがクリアされる学習用アプリケーションを開発したいと考えています。次のコードを試してみましたが、現在の 1 つのアプリケーションのキャッシュ メモリがクリアされ、Null Pointer Exception がスローされます。すべてのアプリケーションのキャッシュ メモリを同時にクリアする必要があります。この問題を解決するのを手伝ってください。よろしくお願いします..
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> mainList = getPackageManager().queryIntentActivities(mainIntent, 0);
Context context;
for(ResolveInfo rInfo : mainList)
{
String packageName = rInfo.activityInfo.applicationInfo.packageName;
context = createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
clearApplicationData(context);
}
public void clearApplicationData(Context context) {
if(context.getCacheDir()!=null)
{
File cache = context.getCacheDir();
File appDir = new File(cache.getParent());
if(appDir.exists()){
String[] children = appDir.list();
for(String s : children){
if(!s.equals("lib")){
deleteDir(new File(appDir, s));
Log.i("TAG", "File /data/data/APP_PACKAGE/" + s +" DELETED");
}
}
}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}