Pending intent with ONE_SHOT flag

后端 未结 1 1170
忘掉有多难
忘掉有多难 2021-01-02 18:31

Currently I\'ve got this code:

public static void setupAlarm(Context context) {
        Intent myIntent = new Intent(context, Receiver.class);

        Pendi         


        
相关标签:
1条回答
  • 2021-01-02 19:10

    Yes, of course you can create it again. You will get a different PendingIntent from the first one.

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

    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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题