【发布时间】: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