Android - 清理應用程式資料

摘要:Android - 清理應用程式資料


網路解:
http://www.hrupin.com/2011/11/how-to-clear-user-data-in-your-android-application-programmatically 
   
    public void clearApplicationData() {
        File cache = 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()) {
            //Log.e(TAG,"delete storage dir " +dir.getAbsolutePath());
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                File file = new File(dir, children[i]);
                //Log.e(TAG,"delete storage file " +file.getAbsolutePath());
                boolean success = deleteDir(file);
                if (!success) {
                    //Log.e(TAG,"delete storage file fail " +file.getAbsolutePath());
                    return false;
                } 
            }
        }

        return dir.delete();
    }