与ONE_SHOT标志待定意图 [英] Pending intent with ONE_SHOT flag
问题描述
目前我已经有了这个code:
Currently I've got this code:
public static void setupAlarm(Context context) {
Intent myIntent = new Intent(context, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, myIntent, PendingIntent.FLAG_NO_CREATE);
if (pendingIntent != null) {
return;
} else {
pendingIntent = PendingIntent.getBroadcast(context, PENDING_INTENT_RETRY, myIntent,
PendingIntent.FLAG_ONE_SHOT);
}
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 2);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
我要的是使用一个时间待定意图和等待火灾。如果在此期间,有人问新的报警,如果报警存在,我不希望任何设置。现在我的问题:第一次报警后,挂起的意图是由于ONE_SHOT标志删除,但我可以再和不创建挂起的意图
What I want is to use pending intent one time and wait for the fire. If in the meantime someone asks for a new alarm, if the alarm exist I don't want to setup anything. Now my question: after the first alarm, the pending intent is deleted due to the ONE_SHOT flag, but can I create the pending intent again or not?
推荐答案
是的,当然你可以重新创建它。你会得到不同的的PendingIntent
从第一个。
Yes, of course you can create it again. You will get a different PendingIntent
from the first one.
不过,也有与您发布的code一些问题。首先,创建的PendingIntent
是这样的:
However, there are a few problems with the code you've posted. First of all, you create the PendingIntent
like this:
pendingIntent = PendingIntent.getBroadcast(context,
PENDING_INTENT_RETRY, myIntent, PendingIntent.FLAG_ONE_SHOT);
但你检查它是否存在这样的:
but you check if it exists like this:
pendingIntent = PendingIntent.getBroadcast(context,
0, myIntent, PendingIntent.FLAG_NO_CREATE);
这将检查始终返回null ,因为你使用的是不同的请求code
!当您创建的PendingIntent
你通过 PENDING_INTENT_RETRY
为请求code
,但是当你检查它是否存在你传递0作为请求code
。
This check will always return null because you are using a different requestCode
! When you create the PendingIntent
you pass PENDING_INTENT_RETRY
as requestCode
, but when you check if it exists you pass 0 as requestCode
.
第二个问题是 FLAG_ONE_SHOT
的工作方式。如果您创建一个的PendingIntent
使用 FLAG_ONE_SHOT
,然后尝试得到一个的PendingIntent
使用 FLAG_NO_CREATE
,将始终返回null,即使的PendingIntent
尚未使用!由于此行为,则不能使用 FLAG_NO_CREATE
,以确定是否报警等待如果您已设置使用警报的的PendingIntent
与 FLAG_ONE_SHOT
创建。
The second problem is the way FLAG_ONE_SHOT
works. If you create a PendingIntent
using FLAG_ONE_SHOT
and then try to get a PendingIntent
using FLAG_NO_CREATE
, it will always return null, even if the PendingIntent
has not yet been used! Because of this behaviour, you cannot use FLAG_NO_CREATE
to determine if an alarm is pending, if you have set that alarm using a PendingIntent
created with FLAG_ONE_SHOT
.
如果你真的想用这个架构,那么你就不能使用 FLAG_ONE_SHOT
。刚刚创建的PendingIntent
(通常不旗),并使用检查其存在 FLAG_NO_CREATE
。
If you really want to use this architecture, then you cannot use FLAG_ONE_SHOT
. Just create the PendingIntent
normally (without flags) and check for its existence using FLAG_NO_CREATE
.
这篇关于与ONE_SHOT标志待定意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!