728x90
    
    
  반응형
    
    
    
  public static void clearApplicationData(Context context) {
        File cache = context.getCacheDir();
        File appDir = new File(cache.getParent());
        if (appDir.exists()) {
            String[] children = appDir.list();
            for (String list : children) {
                Log.d("캐시삭제 ", list);
                if(list.equals("shared_prefs")) continue;
                deleteDir(new File(appDir, list));
            }
        }
    }
    private 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();
    }
이렇게 코드가 있는데요.
우선 주목하실것이 clearApplicationData 함수의 로그와 조건문 입니다.
해당 로그를 한번 보시면
캐시삭제: cache 
캐시삭제: shared_prefs 
캐시삭제: app_webview 
캐시삭제: code_cache 
캐시삭제: files 
캐시삭제: no_backup 
캐시삭제: databases 
캐시삭제: app_textures
그럼 이중 내가 삭제하지 말아야 할 부분을 빼고 삭제해야 한다고 하시면
if(list.equals("shared_prefs")) continue;
조건문에 이처럼 적어주시면 됩니다.
저는 쉐어드프리퍼런스 값을 남기고 삭제하고싶어서 이렇게 조건을 작성했습니다.
1개가 아니라 2개이상이시면 그 밑에 else if로 쭉 나열해서 적어주시면 해당부분은 반복문에서 컨티뉴로 건너뛰고 실행되니,
삭제가 안됩니다.
사용 하실 때는
clearApplicationData(getApplicationContext());
이렇게 적어주시면 끝입니다.
728x90
    
    
  반응형
    
    
    
  'Android' 카테고리의 다른 글
| <Kotlin 코틀린> 안드로이드 하이브리드 Alert창 처리하기 (0) | 2021.11.10 | 
|---|---|
| <Kotlin 코틀린> 안드로이드 WebView(웹뷰) window.open 새창 처리하기 (옆에 짤림 해결) (0) | 2021.11.10 | 
| android11 intent 패키지명으로 외부 앱 실행 시 안되는 현상 (0) | 2021.07.22 | 
| Android (타이머) 시간마다 실행하기 (주기별 실행) (0) | 2021.07.02 | 
| Android polling(폴링) 백그라운드에서 API통신 해보기. (0) | 2021.07.02 | 
댓글