3

私はアンドロイドに非常に慣れていません。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();
} 
4

2 に答える 2

-1

private void clearAllCache() {
		PackageManager  pm = getPackageManager();
		Method[] methods = pm.getClass().getDeclaredMethods();
		for (Method m : methods) {
			if (m.getName().equals("freeStorage")) {
				try {
					long desiredFreeStorage = Long.MAX_VALUE;
					m.invoke(pm, desiredFreeStorage , null);
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			}
		}
}

于 2015-06-10T05:09:18.577 に答える
-3

ClearApplicationData を配置する代わりに、ClearApplicationDataAll のようなものを配置するか、すべてのアプリケーションを対象とする場所にします。

于 2013-03-18T23:01:06.313 に答える