使用PendingIntent传递数据 [英] Passing data with a PendingIntent

查看:233
本文介绍了使用PendingIntent传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发出一条消息已到达的通知.我添加了一个动作,希望在通知上显示一个图标(小红球).我希望如果用户击中redredball,则主要活动将启动,并且该活动(检查Extras捆绑包)将看到与常规启动不同的命令.

I am attempting to raise a notification that a message has arrived. I have added an action expecting an icon (smallredball) to show on the notification. I expect that if the user hits smallredball, main activity will startup and the activity, checking the extras bundle, will see the orders to do something different than if it were just started up normally.

该通知会与文本一起显示在目标电话(运行KitKat)上,但不会显示小红球图标.当用户触摸通知时,活动将不加执行地执行.活动现在正在获得额外的捆绑.

The notification shows on the target phone (running KitKat) along with the text but the smallredball icon never shows. When the user touches the notification the activity executes with no extra. THE ACTIVITY IS NOW GETTING THE EXTRA BUNDLE.

这是发送通知的代码:

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();

        }

为什么通知中没有显示图标?由于我将AutoCancel设置为true,所以我希望仅触摸通知即可使其消失.但是,它运行的应用程序没有提供额外的捆绑包吗? 谢谢, 院长

Why isn't the icon showing in the notification? Since I setAutoCancel to true, I expected that simply touching the notification would make it just go away. But instead it runs the app providing no extra bundle? Thanks, Dean

推荐答案

由于解决该问题和我所遇到的类似问题的要点在该主题中有所分散,因此,这是我的两点备忘单:

Since the points that will solve this problem and similar problems I have had, are spread around a bit in that topic, here is my two point cheat sheet:

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

Point 1: use code like the following to create a pending intent. The choice of flags in the last argument is important:

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

第2点:挂起的意图存储在全局系统表中,并且仅从其中创建意图的某些部分是用于在此表中查找内容的关键字"的一部分.额外内容不是关键,因此,如果您希望两个意图映射到两个不同的挂起意图,请确保它们在其他方面有所不同,例如具有不同的操作,数据或类型.

Point 2: pending intents are stored in a global system table, and only certain parts of the intent they are created from are part of the "key" that is used to look things up in this table. Extras are not part of the key, so if you want two intents to map to two different pending intents, make sure they are different in some other way, for example having different actions, data, or types.

此示例更改了操作:

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

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

(The action can be anything as long as it is different than what you use with the same class elsewhere. )

Javadoc的最新版本中有一个很好的解释,用于说明未决的意图. ,特别是我引出的这句话:

There is a good explanation in the latest version of the Javadoc for pending intents., especially this quote I pulled out:

...重要的是要知道何时将两个Intent视为相同,以便检索PendingIntent.人们经常犯的一个错误是创建多个PendingIntent对象,它们的Intent仅在其额外"内容中有所不同,期望每次都获得一个不同的PendingIntent.这不会发生.用于匹配的Intent部分与Intent.filterEquals定义的部分相同.

... 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.

这篇关于使用PendingIntent传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆