2017-03-01 2 views
0

앱을 강제 종료하지 않고 앱 캐시를 지워야합니다. 계속 실행하려면 앱이 필요합니다. 응용 프로그램을 테스트하기 위해 에스프레소를 사용하고 있지만 응용 프로그램을 시작하기 전에 캐시를 지워야합니다. 그것을 할 수있는 방법이 있습니까?앱을 강제로 저장하지 않고 캐시 지우기

public static void clearPreferences(Activity activity) { 
    try { 
     // clearing app data 
     String packageName = activity.getPackageName(); 
     Runtime runtime = Runtime.getRuntime(); 
     runtime.exec("pm clear "+packageName); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

이것은 내가 가진 것입니다. 하지만 앱을 종료하고 테스트 케이스를 종료합니다.

+0

menifest에하는 File''처럼, 일반 자바 I/O 클래스를 사용하여,') (getCacheDir'의 모든 파일을 삭제합니다. – CommonsWare

+0

이 링크를 확인하십시오. http://stackoverflow.com/questions/23908189/clear-cache-in-android-application-programmatically –

답변

0

이 링크는 도움이되는 데 도움이됩니다.

http://stackoverflow.com/questions/23908189/clear-cache-in-android-application-programmatically 


public static void deleteCache(Context context) { 
    try { 
     File dir = context.getCacheDir(); 
     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(); 
    } else if(dir!= null && dir.isFile()) { 
     return dir.delete(); 
    } else { 
     return false; 
    } 
} 

추가 권한을