Android清理应用程序数据

时间:2012-02-02 09:44:41

标签: android clear

我正在开发一个应用程序,其中有一个按钮,它将清除与此应用程序相关的所有数据(包括文件,数据库,共享首选项)。任何人都可以让我知道如何通过代码清除应用程序数据。

1 个答案:

答案 0 :(得分:6)

致电clearApplicationData删除应用的数据:

/**
 * Call this method to delete any cache created by app
 * @param context context for your application
 */
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 s : children) {
            File f = new File(appDir, s);
            if(deleteDir(f))
                Log.i(TAG, String.format("**************** DELETED -> (%s) *******************", f.getAbsolutePath()));
        }
    }
}
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();
}