2016-10-24 5 views
-1

でプログラムで消去する方法marsmallowのすべてのアプリケーションのキャッシュデータを消去する方法。 与えられる権限とデータの削除方法は何ですか? コードはmarshmallowの下で動作していますが、marshmallowでは動作していません。インストールされているすべてのアプリケーションのキャッシュデータをプログラムでMarshmallowバージョン

if (mClearCacheObserver == null) 
{ 
    mClearCacheObserver=new CachePackageDataObserver(); 
} 

PackageManager mPM=getPackageManager(); 

@SuppressWarnings("rawtypes") 
final Class[] classes= { Long.TYPE, IPackageDataObserver.class }; 

Long localLong=Long.valueOf(CACHE_APP); 

try 
{ 
    Method localMethod = mPM.getClass().getMethod("freeStorageAndNotify", classes); 
    localMethod.setAccessible(true); 

    // Start of inner try-catch block 
    try 
    { 
     localMethod.invoke(mPM, localLong, 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(); 
    } 
    // End of inner try-catch block 
} 
catch (NoSuchMethodException e1) 
{ 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} 
+0

何...あなたが –

答えて

1

ちょうどそれを試してみてください。

PackageManager pm = getPackageManager(); 
    // Get all methods on the PackageManager 
    Method[] methods = pm.getClass().getDeclaredMethods(); 
    for (Method m : methods) { 
     if (m.getName().equals("freeStorage")) { 
      // Found the method I want to use 
      try { 
       long desiredFreeStorage = 8 * 1024 * 1024 * 1024; // Request for 8GB of free space 
       m.invoke(pm, desiredFreeStorage , null); 
      } catch (Exception e) { 
       // Method invocation failed. Could be a permission problem 
      } 
      break; 
     } 
    } 

あなたのマニフェストにこれを持っている必要があります。

<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/> 

link

(または)

てみてください参照してくださいこの 1つのアプリケーションキャッシュ

public static void deleteCache(Context context) { 
     try { 
      File dir = context.getCacheDir(); 
      if (dir != null && dir.isDirectory()) { 
       deleteDir(dir); 
      } 
     } catch (Exception e) {} 
    } 

    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(); 
    } 

ためにも、これを試してみてください:私が行っている間違いです

public static void trimCache(Context context) { 
    File dir = context.getCacheDir(); 
    if(dir!= null && dir.isDirectory()){ 
     File[] children = dir.listFiles(); 
     if (children == null) { 
      // Either dir does not exist or is not a directory 
     } else { 
      File temp; 
      for (int i = 0; i < children.length; i++) { 
       temp = children[i]; 
       temp.delete(); 
      } 
     } 

    } 

} 
+0

上記のコードは、私が教えてください –

+0

すでに試してみましたが動作していない私に説明してくださいすることができます私はきれいにする別の方法 –

+0

上記の私の答えを確認してください、私はそれを更新し、あなたにお試しください –

関連する問題