【发布时间】:2016-06-02 16:50:28
【问题描述】:
我在活动类中有一个值。我想在非活动类中使用该值。通常,为了在活动类之间共享数据,我使用 like,
FirstActivityClass.java
SharedPreferences notification_id = getSharedPreferences("NOTIFICATION_ID", MODE_PRIVATE);
SharedPreferences.Editor notificationIDEditor = notification_id.edit();
notificationIDEditor.putString("notification_id", notificationId)
notificationIDEditor.apply();
并在另一个类中检索 notification_id 的值,
SecondActivityClass.java
SharedPreferences notificationIDSharedRetrieve = getSharedPreferences("NOTIFICATION_ID", MODE_PRIVATE);
notificationID = notificationIDSharedRetrieve .getString("notification_id", null);
但是假设第二个类是非活动类,我如何检索非活动类中的数据?
【问题讨论】:
-
如果
second class was a non-activity class然后将 Context 传递给第二类以访问getSharedPreferences方法 -
一些语法或有关此方法的知识会有所帮助。
-
也许this 有帮助
标签: java android android-activity sharedpreferences