【问题标题】:Adding flags to PendingIntent将标志添加到 PendingIntent
【发布时间】:2014-09-11 16:42:45
【问题描述】:

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

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

它是否遵循任何标志规则意味着 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 活动中打印数字值,其中数字值将变为 80 而不是预期的 50,因为它应该使用具有旧值的现有意图(根据我的理解)。请更新输出为 80 的原因。 日志显示对象与预期相同。

谢谢

【问题讨论】:

    标签: android android-pendingintent


    【解决方案1】:

    当你打电话时:

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

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


    如果你打电话:

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

    再次,您传递的Intent 与第一次调用中的Intent 匹配,那么您将返回与第一次调用相同的PendingIntent。 “匹配”意味着 ACTION、DATA、CATEGORY 和 COMPONENT 都相同。匹配时不考虑额外内容。

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


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

    【讨论】:

    • 谢谢@Wasser。这确实很有帮助,我现在不记得我是否能够解决这个问题,因为我已经有一段时间没有解决这个问题了。无论如何,你的解释仍然澄清了很多关于这个的事情。真的很有帮助。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 2010-09-21
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多