我想在我的应用程序中应用注销功能。在这里,我想清除数据库和sharedPreferences中的所有用户数据。如果我手动执行即:(设置 - >应用程序 - >应用程序名称 - >清除数据)我得到我想要的。
我尝试使用以下代码以编程方式清除数据,但它不会清除完整数据,以前的用户详细信息仍然占上风。
这是我的代码:
public class ClearData extends Application
{
private static ClearData instance;
@Override
public void onCreate()
{
super.onCreate();
instance = this;
}
public static ClearData getInstance()
{
if(instance == null)
instance = new ClearData();
return instance;
}
public void clearApplicationData(Context mContext)
{
File cache = mContext.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()) {
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();
}
}
我将数据清除为:
editor.commit();
ClearData.getInstance().clearApplicationData(_context);
我希望行为与手动过程相同。我可以遵循的任何其他方法吗?
答案 0 :(得分:1)
尝试这种方式:这将删除您的所有文件夹。您只需要传递文件夹路径..
File file = new File(Your_folder_path);
if (file.exists()) {
String deleteCmd = "rm -r " + path;
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(deleteCmd);
} catch (Exception e) {
e.printStackTrace();
}
}