【问题标题】:Shared Preferences Not Applying Changes共享首选项未应用更改
【发布时间】:2017-11-15 22:33:05
【问题描述】:

我有一个活动 (A) 来检查我的服务器是否有 APK 更新。调用此函数时,无论是否有更新,我都需要编辑共享首选项,以便应用程序知道跳过或运行实际的更新活动 (B)。问题 1 是,为什么共享偏好没有被活动 (A) 编辑?问题 2 是,如果它们正在被编辑,为什么活动 (B) 不阅读它们?

提前谢谢你!

应在此处编辑共享首选项(活动 (A)):

    private void parseJson(String result) {
    try {

        JSONObject obj = new JSONObject(result);
        String updateMessage = obj.getString(Constants.APK_UPDATE_CONTENT);
        String apkUrl = obj.getString(Constants.APK_DOWNLOAD_URL);
        int apkCode = obj.getInt(Constants.APK_VERSION_CODE);

        int versionCode = AppUtils.getVersionCode(mContext);

        if (apkCode > versionCode) {
            if (mType == Constants.TYPE_NOTIFICATION) {
                showNotification(mContext, updateMessage, apkUrl);
            } else if (mType == Constants.TYPE_DIALOG) {
                SharedPreferences pref = mContext.getSharedPreferences("ActivityUpdatePREF", Context.MODE_PRIVATE);
                SharedPreferences.Editor ed = pref.edit();
                ed.putBoolean("activity_update", true);
                ed.apply();
                showDialog(mContext, updateMessage, apkUrl);
            }
        } else if (mShowProgressDialog) {
            SharedPreferences pref = mContext.getSharedPreferences("ActivityUpdatePREF", Context.MODE_PRIVATE);
            SharedPreferences.Editor ed = pref.edit();
            ed.putBoolean("activity_update", false);
            ed.apply();
            Toast.makeText(mContext, mContext.getString(R.string.android_auto_update_toast_no_new_update), Toast.LENGTH_SHORT).show();
        }

    } catch (JSONException e) {
        Log.e(Constants.TAG, "parse json error");
    }
}

活动(B):

        SharedPreferences pref = getSharedPreferences("ActivityUpdatePREF", Context.MODE_PRIVATE);
    if(pref.getBoolean("activity_update", false)){
        Intent intent = new Intent(this, Main.class);
        deleteCache(getApplicationContext());
        startActivity(intent);
        finish();
    } else {
        functionUp();
    }

【问题讨论】:

  • 可能是错字,我在活动 a 中看到 ActivityupdatePREF,在活动 b 中看到 ActivityPREF
  • @RobertI 哇,真不敢相信我错过了。我已修复并正在等待应用程序编译并立即运行以进行测试。谢谢!
  • @RobertI 不幸的是,它没有解决问题。我已经更新了上面的代码。
  • 能不能在调用activityb的时候放代码?
  • 你确定调用了 et.apply 或 et.commit 吗?

标签: java android android-activity sharedpreferences


【解决方案1】:

使用 commit() 而不是 apply()

与 commit() 不同,commit() 将其偏好写入持久化 同步存储,apply() 将其更改提交到内存中 SharedPreferences 立即但开始异步提交 磁盘,您将不会收到任何故障通知。如果另一个编辑 此 SharedPreferences 执行常规 commit() 而 apply() 是 仍然未完成,commit() 将阻塞,直到所有异步提交都完成 完成以及提交本身。

https://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply()

【讨论】:

  • 所以我应该尝试将其更改为 commit()?
  • 是的,试试这个,然后告诉我
  • 刚刚运行它,没有运气
【解决方案2】:

你的代码没问题,你应该检查你ed.apply();被执行的是你的代码。

Activity(A),你应该检查程序的执行顺序。

1.确保parseJson方法被执行。

2.如果解析json错误,请检查JSONException

Log.e(Constants.TAG, "parse json error");
e.printStackTrace();

3.如果apkCode > versionCode被执行了,mType == Constants.TYPE_DIALOG被执行了吗?

4.如果apkCode < versionCode被执行,你的代码中mShowProgressDialog是真的吗?

你可以添加Log来查看。

ed.apply();
Log.e(Constants.TAG, "SharedPreferences is apply");

【讨论】:

    【解决方案3】:

    使用 ed.commit() 代替 ed.apply()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 2012-03-29
      • 1970-01-01
      相关资源
      最近更新 更多