如何清除整个应用程序数据而不强制关闭Android中的应用程序

时间:2017-08-31 07:40:20

标签: android

我想清除整个应用数据,例如

try {
  // clearing app data
  String packageName = getActivity().getPackageName();
  Runtime runtime = Runtime.getRuntime();
  runtime.exec("pm clear " + packageName);
} catch (Exception e) {
  e.printStackTrace();
}

我尝试了这段代码,但强行关闭了应用。

npm install moment-timezone --save

请帮我解决问题,谢谢。

3 个答案:

答案 0 :(得分:3)

尝试这种方式清除缓存和应用程序数据(在棒棒糖上完美地尝试和工作): -

(1)ClearApplicationData课程下面添加,将progressDialog中的 AcivityName 您的sharedPreference 名称更改为使用编辑器清除: -

private class ClearApplicationData extends AsyncTask<Void, String, String> {

    ProgressDialog mDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mDialog = new ProgressDialog(MainActivity.this); //Change MainActivity as per your activity
        mDialog.setMessage("Data is clearing...");
        mDialog.setCancelable(false);
        mDialog.show();
    }

    protected String doInBackground(Void... urls) {
        SharedPreferences.Editor editor = mContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).edit();
        editor.clear();
        editor.apply();
        File cacheDir = mContext.getCacheDir();
        File appDir = new File(cacheDir.getParent());
        deleteRecursive(appDir);
        return "";
    }

    protected void onPostExecute(String result) {
        mDialog.dismiss();
        Toast.makeText(mContext, "Data is cleared.", Toast.LENGTH_SHORT).show();
        finish();  //Activity finish
    }
}

(2)添加以下函数以递归方式删除文件: -

public void deleteRecursive(File fileOrDirectory) {

    if (fileOrDirectory.isDirectory()) {
        for (File child : fileOrDirectory.listFiles()) {
            deleteRecursive(child);
        }
    }
    fileOrDirectory.delete();
}
当您要清除应用程序数据时,

(3)调用该类: -

new ClearApplicationData().execute();

答案 1 :(得分:0)

/**You can clear the data programmatically as well without affecting
   Shared Preferences or data base data**/

 public static void deleteCache(Context context)
{
    try
    {
        File dir = context.getCacheDir();
        deleteDir(dir);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

private static boolean deleteDir(File dir)
{
    if (dir != null && dir.isDirectory())
    {
        String[] children = dir.list();
        for (String aChildren : children)
        {
            boolean success = deleteDir(new File(dir, aChildren));

            if (!success)
            {
                return false;
            }
        }
        return dir.delete();
    }
    else
    return dir != null && dir.isFile() && dir.delete();
}

答案 2 :(得分:0)

这是在Kotlin中实现的一种方法:

fun clearData() {
    val cache = getCacheDir()
    val appDir = File(cache.getParent())
    if (appDir.exists()) {
        val children = appDir.list();
        for (s in children) {
            if (!s.equals("lib")) {
                deleteDir(File(appDir, s))
                Log.i("TAG", "File /data/data/APP_PACKAGE/" + s + " DELETED")
            }
        }
    }
}

fun deleteDir( dir : File?) : Boolean {
    if (dir != null && dir.isDirectory()) {
        val children = dir.list()
        for ( i in children) {
            val success = deleteDir( File(dir, i));
            if (!success) {
                return false
            }
        }
    }

    return dir!!.delete()
}