gpt4 book ai didi

android - 将标志添加到 PendingIntent

转载 作者:太空宇宙 更新时间:2023-11-03 12:33:42 28 4
gpt4 key购买 nike

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

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

does it follow any flags rules 表示0默认对应任何标志

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

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

它会和之前一样吗?如果我对 Intent 中的数据进行任何更改,它现在会对应于 Intent 中的新数据还是仍然有旧数据。

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

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

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

谢谢

最佳答案

当你打电话时:

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

传递 0 作为标志参数意味着您没有设置任何标志。


如果你调用:

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

同样,如果您传递的 Intent 与第一次调用的 Intent 相匹配,那么您将获得与从第一次打电话。 “匹配”意味着ACTION、DATA、CATEGORY 和COMPONENT 都相同。匹配时不考虑额外内容。

如果您在 Intent 中为第二次调用提供了不同的额外内容,则这些额外内容在发送时不会出现在 PendingIntent 中。将使用第一次调用的 Intent 中的额外内容。


我无法解释您所看到的关于“50”和“80”的行为。根据文档和我的理解以及我自己的观察,您应该看到“50”而不是“80”。一定有其他奇怪的事情发生了。你是在真机上测试吗?模拟器?什么版本的 Android?

关于android - 将标志添加到 PendingIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25792843/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com