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