【问题标题】:Android - on click notification save string in Shared PreferencesAndroid - 在共享首选项中单击通知保存字符串
【发布时间】:2021-05-31 13:32:13
【问题描述】:

我在 Android 开发方面仍然不是很好,我试图弄清楚在我点击 Android 中的通知后如何触发一些操作(在我的情况下,我想在共享首选项中保存一个变量) .

我目前有这个代码:

private void startForegroundService() {
        Log.d(TAG, "Start " + NOTIFICATION_CHANNEL_NAME + " foreground service");
        setNotificationLanguage();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(getResourceIdForResourceName(this, "ic_stat_show_chart"))
                .setSound(null)
                .setContentTitle(NOTIFICATION_CHANNEL_NAME)
                .setContentText(NOTIFICATION_CHANNEL_DESC)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent);
        Notification notification = builder.build();
}

当我单击通知时,我会启动我的 MainActivity 类,这也是我想要的。但我还想在单击通知的共享首选项中保存一个字符串。我已经搜索过了,一无所获。如果有人可以提供帮助,我将不胜感激。谢谢你:)

【问题讨论】:

  • 检查主要活动的意图
  • 尝试以我编码的方式工作。如果它有效@Johny,您可以接受我的回答。谢谢。

标签: android android-notifications


【解决方案1】:

正如 Keshav 解释的,您可以从 Intent 中获取数据并将其存储在 sharedpreferences 中,这里是 SharedPreferenceHelper 类。

public class SharedPreferenceHelper {

 private static final String STRING_NAME = "your_string_name";

 public SharedPreferenceHelper(Context context) {
    mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
  }

 public void setString(String your_string){
    mPreferences.edit().putString(STRING_NAME,your_string).apply();
}

public String getString(){
    return mPreferences.getString(STRING_NAME,null);
}

}

在 MainActivity 中,

public class MainActivity extends AppCompatActivity{

 Bundle mBundle;
 SharedPreferenceHelper mSharedPreferenceHelper;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSharedPreferenceHelper = new SharedPreferenceHelper(this);
    mBundle = getIntent().getExtras();

    if(mBundle != null){
     mSharedPreferenceHelper.setString(mBundle.getString("your_string_data"));
     }

}

如果活动已经创建,则

@Override
protected void onResume() {
    super.onResume();
  if(mBundle != null){
     mSharedPreferenceHelper.setString(mBundle.getString("your_string_data"));
     }
}

enter code here

如果您有任何问题,请告诉我。

【讨论】:

  • 如果 MainActivity 已经创建了怎么办?例如,我是否应该在 onResume() 中也这样做?
  • 是的,但在你输入偏好值@JohnyBoy 之前检查捆绑包是否为空
  • 好的,如果可以的话,更新你的答案,所以我接受它。我认为这很重要。谢谢你的帮助。
  • 当然。谢谢@JohnyBoy
【解决方案2】:

尝试在您的主要活动中执行以下操作:

Intent intent = getIntent();
if (intent.hasExtras()){
   //Saved notification has been clicked to shared preferences
}

希望这可能会有所帮助

【讨论】:

    【解决方案3】:

    您可以将参数传递给意图,并在 MainActivity 检查意图是否有参数 Passing arguments to intent android

    【讨论】:

      【解决方案4】:

      notificationIntent 中添加您想要将 sharedpref 保存为额外的字符串值,如下所示。

      notificationIntent.putExtra("Key","Value");
      

      您将能够在您的 Activity 的onCreate 中提取它。

       String value = getIntent().getStringArrayExtra("Key");
      

      注意:如果您只想知道活动是否通过通知打开,只需在意图中传递任何常量值并使用上述方法检查您是否在onCreate 中获得了该值,即您确定活动是通过通知启动的方式。只需确保在这种情况下,不要将相同的键/值组合用作相同活动意图的附加项,以免从通知pendingIntent 流以外的任何其他地方启动它。

      【讨论】:

        【解决方案5】:

        在 MainActivity 中重写 onNewIntent 方法

        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("KEY", "str value"); //change this according to your need
            editor.apply();
        }
        

        【讨论】:

        • 你完全正确。最好使用 onNewIntent,因为 onCreate 方法仅在 MainActivity 尚未运行时触发。非常感谢!!
        猜你喜欢
        • 2022-01-06
        • 1970-01-01
        • 2012-03-29
        • 1970-01-01
        • 2017-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-22
        相关资源
        最近更新 更多