android– 使用PendingIntent传递数据

android– 使用PendingIntent传递数据,第1张

概述我正在尝试发出消息已到达的通知.我添加了一个动作,期望在通知上显示一个图标(smallredball).我希望如果用户点击smallredball,主要活动将启动,并且检查附加组件的活动将看到订单执行与正常启动时不同的 *** 作.通知显示在目标手机(运行KitKat)和文本上,但小球图标从不显示.当用户触

我正在尝试发出消息已到达的通知.我添加了一个动作,期望在通知上显示一个图标(smallredball).我希望如果用户点击smallredball,主要活动将启动,并且检查附加组件的活动将看到订单执行与正常启动时不同的 *** 作.

通知显示在目标手机(运行KitKat)和文本上,但小球图标从不显示.当用户触摸通知时,活动无需额外执行.编辑:活动现在正在获得额外的捆绑.

这是发送通知的代码:

private voID raiseNotification( String username, String mesText) {    DeBUGLog.deBUGLog("GCMIntentService: Attempting to Raise Notification ", false);    NotificationCompat.Builder b = new NotificationCompat.Builder(this);    Intent intent = new Intent(this, MainActivity.class);    intent.putExtra("whattodo", "showmessage");    intent.setAction(Long.toString(System.currentTimeMillis())); //just to make it unique from the next one    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);    b.setContentTitle("New SafeTalk Message")    .setSmallicon(R.drawable.note24x24)    .setContentText("From " + username + "  " + mesText)    .setTicker("New SafeTalk Message")    .setContentIntent(pIntent)    .setSound(ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION))    .setautoCancel(true)    .addAction(R.drawable.smallredball, "Read Now", pIntent);     notificationmanager mgr = (notificationmanager)getSystemService(NOTIFICATION_SERVICE);     mgr.notify(0, b.build());      }

这是活动的代码段:

        Bundle extras = getIntent().getExtras();        if (extras == null)        {            GlobalStuff.mpBad.start();        }        else        {            String myOrders =   extras.getString("whattodo");        if (myOrders.equals("showmessage"))            GlobalStuff.mpBeep.start();        }

为什么通知中没有显示图标?由于我将setautoCancel设置为true,我预计只需触摸通知就会让它消失.但相反,它运行的应用程序不提供额外的捆绑?
谢谢,
院长

解决方法:

该主题在an existing question中介绍

既然解决了这个问题的点和我遇到的类似问题,在这个主题中有点散布,这是我的两点备忘单:

第1点:使用如下代码创建待定意图.最后一个参数中标志的选择很重要:

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

第2点:待定意图存储在全局系统表中,并且只有它们创建的意图的某些部分是用于在此表中查找内容的“键”的一部分.附加项不是键的一部分,因此如果您希望两个意图映射到两个不同的待定意图,请确保它们以其他方式不同,例如具有不同的 *** 作,数据或类型.

此示例更改 *** 作:

Intent intent = new Intent(this, MainActivity.class);intent.putExtra("whattodo", "showmessage");// add this:intent.setAction("showmessage");

(该动作可以是任何动作,只要它与您在其他地方使用同一类的动作不同.
)

在latest version of the Javadoc for pending intents.中有一个很好的解释,特别是我引用的这句话:

… it is important to kNow when two Intents are consIDered to be the same for purposes of retrIEving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their “extra” contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals.

总结

以上是内存溢出为你收集整理的android – 使用PendingIntent传递数据全部内容,希望文章能够帮你解决android – 使用PendingIntent传递数据所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存