如何以编程方式清除android应用程序中的数据

时间:2014-04-01 09:53:02

标签: android

我在android应用程序中使用以下代码来清除数据,但它没有清除所有数据。当我在使用此代码后看到应用信息时,它会显示30kb的数据。我在stackoverflow中也看到了很多教程,但是我无法清除应用程序中的所有数据。

 public void clearApplicationData() {
      File cache = getCacheDir();
      File appDir = new File(cache.getParent());
      if(appDir.exists()){
         //Log.i("TAG", "check the delete data==" +" DELETED");
       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()) {
         String[] children = dir.list();
         for (int i = 0; i < children.length; i++) {
             Log.i("TAG", "check the delete data==" + children[i] +" DELETED count="+ i+" length="+children.length );
             boolean success = deleteDir(new File(dir, children[i]));
             if (!success) {
                 return false;
             }
         }
     }
   return dir.delete();

}

1 个答案:

答案 0 :(得分:0)

private void clearPreferences(){

try {

    // clearing app data
    Runtime runtime = Runtime.getRuntime();
    runtime.exec("pm clear YOUR_APP_PACKAGE_GOES HERE");

} catch (Exception e) {
    e.printStackTrace();
}

}

当然可以解决这个问题。