将标志添加到PendingIntent [英] Adding flags to PendingIntent

查看:96
本文介绍了将标志添加到PendingIntent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们将0作为标志传递给PendingIntent时,如下所示:

when we pass 0 as flag to PendingIntent as below :

PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0);

它是否遵循任何标志规则,这意味着默认情况下0对应于任何标志.

does it follow any flags rules means does 0 correspond to any flag by default.

如果我们将另一个PendingIntent创建为

If we create another PendingIntent as

 PendingIntent pii=PendingIntent.getActivity(this, 1, i, 0);

是否与以前相同,并且如果我对Intent中的数据进行了任何更改,现在它是否将与Intent中的新数据相对应或仍然具有旧数据.

will it be same as earlier and if i make any change to the data in Intent , will it now correspond to new data in Intent or still have old data.

我在这方面面临的另一个问题是 我正在尝试检查标志

Another problem i am facing in this is I am trying to check flag

PendingIntent.FLAG_NO_CREATE

我写了以下代码:

Intent i=new Intent(this,NotifResult.class);

        i.putExtra("number",50);
        PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0);
NotificationCompat.Builder nb=new NotificationCompat.Builder(this);
        nb.setSmallIcon(R.drawable.ic_launcher);
        nb.setTicker("ticker is here");
        nb.setWhen(System.currentTimeMillis())
        .setContentTitle("just the title")
        .setContentText("and the description")
        .setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL)
        .setContentIntent(pi);


Notification notif=nb.build();
        NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(11, notif);

        i.putExtra("number",80);

        PendingIntent pisecond=PendingIntent.getActivity(this, 1, i, PendingIntent.FLAG_NO_CREATE);

        if(pi.equals(pisecond))
            Log.v("check","they are equal");
        else
            Log.v("check", "they are not equal");

        notif.contentIntent=pisecond;

        nm.notify(11, notif);

根据docs,如果存在存在对象,则PendingIntent.FLAG_NO_CREATE不会创建任何新对象. 我正在NotifResult活动中打印数字的值,其中数字值将变为80而不是预期的50,因为它应该使用具有旧值的现有意图(根据我的理解).请更新输出为何达到80的原因. 该日志显示的对象与预期的相等.

As per docs , PendingIntent.FLAG_NO_CREATE does not create any new object if there is an existign object. i am printing value of number in the NotifResult activity wherein number value is coming to be 80 rather than 50 expected as it should use existing intent with old value ( as per my understanding).kindly update why output is coming 80. the log is showing objects to be equal as expected.

谢谢

推荐答案

致电时:

PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0);

0作为flags参数传递表示您未设置任何标志.

passing 0 as the flags parameter means that you are setting no flags.

如果您致电:

PendingIntent pii=PendingIntent.getActivity(this, 1, i, 0);

再次

,并且您传递的Intent与第一个电话的Intent相匹配,然后您将获得与第一个电话相同的PendingIntent. 匹配"是指ACTION,DATA,CATEGORY和COMPONENT都相同.匹配时,不考虑其他.

again, and the Intent you pass matches the Intent from the first call, then you will get back the same PendingIntent as from the first call. "matches" means that the ACTION, DATA, CATEGORY and COMPONENT are all the same. Extras are not considered when matching.

如果您在Intent中为第二个呼叫提供了不同的附加功能,则在发送PendingIntent时这些附加功能将不会出现在PendingIntent中.会使用第一个电话Intent中的附加内容.

If you provide different extras in the Intent for the second call, those extras will NOT be present in the PendingIntent when it is sent. The extras in the Intent from the first call will be used.

我无法解释您看到的有关"50"和"80"的行为.根据文档,根据我的理解以及根据我自己的观察,您应该看到"50"而不是"80".肯定还有其他奇怪的事情正在发生.您是否在真实设备上进行测试?模拟器?哪个版本的Android?

I cannot explain the behaviour you are seeing regarding "50" and "80". As per the docs and as per my understanding and as per my own observations, you should see "50" and not "80". There must be something else strange going on. Are you testing on a real device? an Emulator? What version of Android?

这篇关于将标志添加到PendingIntent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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