0

有没有办法在重新安装应用程序时删除或重置共享偏好数据,而无需卸载应用程序并安装它?

我的意思是,目前,我正在开发一个使用共享首选项的应用程序,但由于我仍在开发它,我在进行更改后继续运行并通过 Eclipse 将应用程序上传到测试手机。目前,我无法从预期过程的一开始(第一次之后)运行该应用程序,而无需卸载旧版本然后再次上传该应用程序。

4

3 回答 3

1

清除以下活动:

Intent intent = new Intent(this, Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

清除您的共享偏好:

SharedPreferences pref = this.getSharedPreferences("mypref", Context.MODE_PRIVATE);
getSharedPreferences("pref", Context.MODE_PRIVATE).edit().clear().commit();
于 2013-10-07T05:49:52.247 回答
1

为此,在您的启动活动 onCreate() 方法中检查共享首选项文件是否存在,如果存在则删除它。然后在您想要的任何地方创建它..您可以像这样检查首选项文件是否存在..

public boolean isFirstTime() {
        return getDatabasePath("your file name").exists();
    }
于 2013-10-07T05:44:06.583 回答
0

使用以下函数检查您的启动活动是否清除首选项:

    SharedPreferences prefs = this.getSharedPreferences("prefs", Context.MODE_PRIVATE);
    if (!prefs.getBoolean("FirstRun", true)) {
        // Not the first time so clear prefs
        prefs.edit().clear().commit();
    } else {
        // Set the value for future runs
        prefs.edit().putBoolean("FirstRun", false).commit();
    }
于 2013-10-07T05:42:26.927 回答