什么是“请求代码"?用于 PendingIntent? [英] What's "requestCode" used for on PendingIntent?

查看:25
本文介绍了什么是“请求代码"?用于 PendingIntent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我通过 AlarmManager 将 PendingIntent 用于警报.

I'm using PendingIntent for alarms via AlarmManager.

问题:

一开始我以为为了取消之前的,我必须提供我之前用过的确切的requestCode来启动警报.

At first I thought that in order to cancel previous ones, I must provide the exact requestCode that I've used before to start the alarm.

但后来我发现我错了,因为 取消 API 说:

But then I've found out I was wrong, as the cancellation API says:

删除任何具有匹配意图的警报.任何类型的任何警报,其意图与此匹配(由 filterEquals(Intent) 定义),将被取消.

Remove any alarms with a matching Intent. Any alarm, of any type, whose Intent matches this one (as defined by filterEquals(Intent)), will be canceled.

看着 "filterEquals",文档说:

looking at "filterEquals", the documentation says:

出于意图的目的,确定两个意图是否相同分辨率(过滤).也就是说,如果他们的动作、数据、类型、类,和类别是一样的.这不会比较任何额外的数据包含在意图中.

Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

所以我不明白requestCode"是什么...

so I don't get what the "requestCode" is for...

问题:

requestCode"有什么用?

What is "requestCode" used for?

如果我使用相同的requestCode"创建多个警报怎么办?它们会相互覆盖吗?

What if I create multiple alarms with the same "requestCode" ? do they override each other?

推荐答案

  1. requestCode 用于稍后检索相同的待处理意图实例(用于取消等).
  2. 是的,我的猜测是警报会相互覆盖.我会保持请求代码的唯一性.
  1. requestCode is used to retrieve the same pending intent instance later on (for cancelling, etc).
  2. Yes, my guess is the alarms will override each other. I would keep the request codes unique.

这篇关于什么是“请求代码"?用于 PendingIntent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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