Android状态栏通知 – 选择通知时打开正确的活动

Android状态栏通知 – 选择通知时打开正确的活动,第1张

概述单击时,我一直遇到通知无法打开/转到正确活动的问题. 我的通知代码(位于扩展服务的类中): Context context = getApplicationContext(); CharSequence contentTitle = "Notification"; CharSequence contentText = "New Notification"; final 单击时,我一直遇到通知无法打开/转到正确活动的问题.

我的通知代码(位于扩展服务的类中):

Context context = getApplicationContext();    CharSequence contentTitle = "Notification";    CharSequence contentText = "New Notification";    final Notification notifyDetails =        new Notification(R.drawable.icon,"ConsIDer yourself notifIEd",System.currentTimeMillis());    Intent notifyIntent = new Intent(context,MainActivity.class);    PendingIntent intent =          PendingIntent.getActivity(context,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_auto_CANCEL);    notifyDetails.setLatestEventInfo(context,contentTitle,contentText,intent);    ((notificationmanager)getSystemService(NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID,notifyDetails);

如果在创建服务的应用程序打开时单击通知,通知将消失(由于FLAG_auto_CANCEL),但活动未切换.

如果我从主屏幕点击通知,通知将消失,我的应用程序将被带到前面,但它仍然保留在进入主屏幕之前打开的活动,而不是进入主屏幕.

我究竟做错了什么?如何指定将被提取的活动?

解决方法 可能实际上回答了我自己的问题:
Intent notifyIntent = new Intent(Intent.ACTION_MAIN);notifyIntent.setClass(getApplicationContext(),Main.class);
总结

以上是内存溢出为你收集整理的Android状态栏通知 – 选择通知时打开正确的活动全部内容,希望文章能够帮你解决Android状态栏通知 – 选择通知时打开正确的活动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1136262.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存