Android清理应用程序数据 [英] Android clean application data

查看:404
本文介绍了Android清理应用程序数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing a app in which there is a button which will clear all the data (which will include files, databases, shared preferences) related with this app. any one can let me know how to clear application data by code.

推荐答案

调用clearApplicationData删除应用程序的数据:

call clearApplicationData to delete app's data:

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

这篇关于Android清理应用程序数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆