gpt4 book ai didi

java - Android 服务通知 onclick

转载 作者:行者123 更新时间:2023-12-01 11:49:02 24 4
gpt4 key购买 nike

我有一个前台服务,我想让通知在用户单击通知时打开 MainActivity。

自定义Service类中创建Notification的方法是:

public void setForeground(final int notificationID, final int notificationIconID,
final String notificationTickerText,
final String notificationTitle, final String notificationText){
Notification notification = new Notification(notificationIconID, notificationTickerText,
System.currentTimeMillis());
notification.setLatestEventInfo(this, notificationTitle,
notificationText, PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()), 0));
startForeground(notificationID, notification);
}

如何修改它以包含打开(如果 MainActivity 当前打开)或在通知单击功能上启动 MainActivity?

最佳答案

如果你想通过点击通知来打开任何Activity,那么你需要实现TaskStackBuilder

Intent resultIntent = new Intent(this, ResultActivity.class);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);

// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0,
PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setContentIntent(resultPendingIntent);

关于java - Android 服务通知 onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28925461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com