【发布时间】:2017-11-06 01:01:29
【问题描述】:
我正在使用 sharedprefs 将变量从接收器传递到活动。 这似乎是正确的,如果我关闭并重新启动应用程序,我可以看到接收器已写入 sharedprefs 并且活动可以读取最后写入的值。 但是当应用程序运行时,变量不会改变。它没有从接收者那里得到值。
这是我从接收者那里收到的代码:
if (PluginBundleManager.isBundleValid(bundle))
{
final String message = bundle.getString(PluginBundleManager.BUNDLE_EXTRA_STRING_MESSAGE);
SharedPreferences sharedPref = context.getSharedPreferences("MyPrefsFile", Context.MODE_PRIVATE);
sharedPref.edit().putString("scrltxt", message).apply();
Toast.makeText(context, message , Toast.LENGTH_LONG).show();
}
这是来自活动的代码:
public void onResume(){
super.onResume();
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences sharedPreferencesx = getApplicationContext().getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);
scrltxt = sharedPreferencesx.getString("scrltxt", "");
Toast.makeText(getApplicationContext(), scrltxt, Toast.LENGTH_LONG).show();
mEdit = (EditText)findViewById(R.id.EditText01);
mEdit.setText(scrltxt);
那么有什么问题吗? 为什么app关闭前receiver不能写?
顺便说一句,我也在同一个应用程序中的单独服务上读取这个共享首选项文件。
【问题讨论】:
标签: android