android - 在 Android 中更改通知 Intent

标签 android android-intent android-service android-notifications

我有一个显示通知的服务,我希望它能够在用户每次按下时转到我的应用程序的特定 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/

相关文章:

java - Http URL 连接无法处理重定向

java - Button.setEnabled 在需要时不启用按钮

android - 从 Android 上的 Google Place API 获取城市名称和邮政编码

java - 以编程方式从 Android 应用程序启动 Skype

android - 非常简单的代码,但是使用 Fragment 时出现错误 "Activity has been destroyed"

java - 一项 Activity 中基于所选 ListView 项的多个上下文菜单

android - 通过从 onDestoy 方法调用 startService 来保持服务持久化

android - Hello World Android IntentService - IntentService 的 enqueueWork() 函数不接受参数 MyIntentService.class

android - 如何从服务内部检测/监听 Android 应用程序/Activity 的运行变化?

Android:Sms Retrieval Api 未从 Intent 获取消息