我有一个显示通知的服务,我希望它能够在用户每次按下时转到我的应用程序的特定 Activity 。通常它是用户显示的最后一个,但并非总是如此。
如果 Activity 之前已经启动,它应该返回它,如果没有,它应该在应用程序的任务中打开它,将它添加到 Activity 任务中。
此外,在某些情况下,根据服务的逻辑,我希望更改通知的 Intent ,以便它针对不同的 Activity 。
我该怎么做?是否可以不创建新通知并关闭前一个通知?是否也可以不创建新任务或 Activity 实例?
最佳答案
不,发送通知后将无法更改 Activity 。
您可以在您的任务堆栈上启动一个 Activity,这不是问题,请在此处查看教程中的通知服务:
http://blog.blundell-apps.com/notification-for-a-user-chosen-time/
您必须在通知上设置一个待处理的 Intent :
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);
// Set the info for the view that shows in the notification panel.
notification.setLatestEventInfo(this, title, text, contentIntent);
您可以看到挂起的 Intent 采用普通 Intent “new Intent(this, SecondActivity.class
”,所以如果您想要特定的行为(比如带到顶部而不是开始新的 Activity 。添加像您通常会为此 Intent 使用的标志。即 FLAG_ACTIVITY_REORDER_TO_FRONT ( something like that )
关于android - 在 Android 中更改通知 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10955484/