澄清对文档进行PendingIntent.FLAG_CANCEL_CURRENT [英] Clarification on the docs for PendingIntent.FLAG_CANCEL_CURRENT

查看:523
本文介绍了澄清对文档进行PendingIntent.FLAG_CANCEL_CURRENT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从待定意向的文档 FLAG_CANCEL_CURRENT 在Android的:

From the documentation of Pending Intent FLAG_CANCEL_CURRENT in Android:

通过取消previous未决的意图,这确保了只有赋予新数据的实体将能够启动它。如果这个保证是不是一个问题,可以考虑FLAG_UPDATE_CURRENT

by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT

谁能解释一下这行的意思?

Can anyone explain what this line means?

推荐答案

在你创建一个新的 PendingIntent FLAG_CANCEL_CURRENT ,什么都拿着previous PendingIntent 为同一意图将不再能够执行该原始 PendingIntent

Once you create a new PendingIntent with FLAG_CANCEL_CURRENT, anything holding a previous PendingIntent for the same Intent will no longer be able to execute that original PendingIntent.

例如,假设我们有这样的:

For example, suppose we have this:

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

i.putExtra("key", 1);

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

和我们使用 PendingIntent 用,也就是说,一个通知

and we use that PendingIntent with, say, a Notification.

后来,我们执行:

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

i.putExtra("key", 2);

PendingIntent pi2=PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);

在这一点上,<​​code> PendingIntent 创建最初( PI )不再有效,而无论我们使用 PI2 为将看到更新的额外价值( 2 )。

At this point, the PendingIntent created originally (pi) is no longer valid, and whatever we use pi2 for will see the updated extra value (2).

如果,相反,我们所做的:

If, instead, we did:

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

i.putExtra("key", 2);

PendingIntent pi2=PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

在这一点上,<​​code> PI 和 PI2 转口present中的一样 PendingIntent ,都将看到更新后的额外价值( 2 )。

At this point, pi and pi2 both represent the same PendingIntent, and both will see the updated extra value (2).

或者,如果我们做的:

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

i.putExtra("key", 2);

PendingIntent pi2=PendingIntent.getActivity(this, 0, i, 0);

在这一点上,<​​code> PI 和 PI2 还是重新present相同 PendingIntent ,但演员不变,如 getActivity()返回的 PendingIntent 不应用新的附加功能。

At this point, pi and pi2 still represent the same PendingIntent, but the extras are unchanged, as getActivity() returns the original PendingIntent without applying the new extras.

大多数时候, FLAG_UPDATE_CURRENT 是当你试图替换 PendingIntent 里面的额外罚款的答案。

Most times, FLAG_UPDATE_CURRENT is a fine answer when you are trying to replace extras inside a PendingIntent.

这篇关于澄清对文档进行PendingIntent.FLAG_CANCEL_CURRENT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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