【问题标题】:save text message in Shared Preferences [duplicate]在共享首选项中保存短信[重复]
【发布时间】:2018-09-16 22:30:24
【问题描述】:

我将 FCM 推送通知发送到我的 android 应用程序,当我单击意图时,我会在“我的活动”中显示消息。查看消息后,当 Activity 被销毁或杀死时,消息不再显示在我的活动中。我想将消息保存在 SharedPreferences 中,以便在我恢复该活动时可以查看它。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    String title = getIntent().getStringExtra("title");
    String message = getIntent().getStringExtra("message");

    setTitle(title);
    TextView desc = (TextView) findViewById(R.id.desc1);
    desc.setText(message);
}

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    我不明白您为什么要这样做,但可以。如果您只需要保存到 SharedPrefs,这很简单

    SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("yourMessageKey", message);
    editor.commit();
    

    当然,要回读给你

    SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    String message = sharedPref.getString("yourMessageKey", defaultValue);
    

    【讨论】:

    • 首先感谢您的评论,转发我的托盘以资助错误并解决它再次感谢您
    • 希望对你有用!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 2013-09-20
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    相关资源
    最近更新 更多