Java 以编程方式清除应用程序数据
我有一个带有SharedReferences的应用程序。我想在点击按钮时清除完整的应用程序数据,这样应用程序就可以像第一次安装时一样重新启动Java 以编程方式清除应用程序数据,java,android,logout,Java,Android,Logout,我有一个带有SharedReferences的应用程序。我想在点击按钮时清除完整的应用程序数据,这样应用程序就可以像第一次安装时一样重新启动 File cache = mContext.getCacheDir(); 我试过: File cache = mContext.getCacheDir(); ClearData.java public class ClearData extends Application { private static ClearData inst
File cache = mContext.getCacheDir();
我试过:
File cache = mContext.getCacheDir();
ClearData.java
public class ClearData extends Application
{
private static ClearData instance;
@Override
public void onCreate() {
super.onCreate();
instance = this;
}
public static ClearData getInstance() {
return instance;
}
public void clearApplicationData() {
File cache = 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/mypackage/" + 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();
}
}
File cache = mContext.getCacheDir();
但是我在上面一行得到了一个NullPointerException不要使用扩展应用程序的类,declare
clearApplicationData(Context mContext)
File cache = mContext.getCacheDir();
而不是
clearApplicationData()
File cache = mContext.getCacheDir();
File cache = getCacheDir();
及
File cache = mContext.getCacheDir();
而不是
clearApplicationData()
File cache = mContext.getCacheDir();
File cache = getCacheDir();
主要活动本身。然后打电话
File cache = mContext.getCacheDir();
clearApplicationData(getBaseContext());
点击按钮btnLogout。希望这有帮助。不要使用扩展应用程序的类,声明
clearApplicationData(Context mContext)
File cache = mContext.getCacheDir();
而不是
clearApplicationData()
File cache = mContext.getCacheDir();
File cache = getCacheDir();
及
File cache = mContext.getCacheDir();
而不是
clearApplicationData()
File cache = mContext.getCacheDir();
File cache = getCacheDir();
主要活动本身。然后打电话
File cache = mContext.getCacheDir();
clearApplicationData(getBaseContext());
点击按钮btnLogout。希望这有帮助。这里有另一种完全清除应用程序数据的方法:
File cache = mContext.getCacheDir();
private void clearAppData() {
try {
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear " + getApplicationContext().getPackageName() + " HERE");
} catch (Exception e) {
e.printStackTrace();
}
}
下面是另一种完全清除应用程序数据的方法:
File cache = mContext.getCacheDir();
private void clearAppData() {
try {
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear " + getApplicationContext().getPackageName() + " HERE");
} catch (Exception e) {
e.printStackTrace();
}
}
为什么不使用SharedReferences.Editor.clear();SharedReferences.Editor.commit()@QAMAR Iam已经在使用它来清除首选项。但不知何故,他们并没有被清除。即使用户注销,而另一个用户登录,我也会获取上一个用户的数据。为了更好地使用,请提交堆栈跟踪/日志visibility@user3034944请发布您的logcat错误。卸载并重新安装您的应用程序,以便“重新”启动。为什么不使用SharedReferences.Editor.clear();SharedReferences.Editor.commit()@QAMAR Iam已经在使用它来清除首选项。但不知何故,他们并没有被清除。即使用户注销,而另一个用户登录,我也会获取上一个用户的数据。为了更好地使用,请提交堆栈跟踪/日志visibility@user3034944请发布你的logcat错误。卸载你的应用程序并重新安装,以获得一个“全新”的开始。良好的描述格式!!代码也工作得很好:)很好的描述格式!!代码也工作得很好:)