Pending intent with different intent but same ID

孤人 提交于 2019-12-12 10:45:59

问题


I have two pending Intent to use with Alarm Manager one is:

Intent i = new Intent(context, TriggerAlarm.class);  
PendingIntent pi =PendingIntent.getBroadcast(context,0,i,PendingIntent.FLAG_CANCEL_CURRENT);

and the other is:

 Intent i = new Intent(context, TriggerNotification.class);
 PendingIntent pi = PendingIntent.getBroadcast(context,0, i,PendingIntent.FLAG_CANCEL_CURRENT);

I use these two in different methods in my application

my question is:

Are these pendingIntents differnt from each other?? because the intents are different but the Ids are same

If I set alarm manager for each of these pending intent do both of them trigger or one replace another?


回答1:


So the easy way is test it directly by yourself. I have tested it on my computer and here is what i got:

Are these pendingIntents different from each other?? because the intents are different but the Ids are same

-Yes they are different each other although the Ids are same

If I set alarm manager for each of these pending intent do both of them trigger or one replace another?

-Both of them will be triggered

Here are my code for test, you can copy and try it by yourself

Copy this method to your activity, and call it

private void setAlarmManager() {
    Log.v("AlarmManager", "Configuring AlarmManager...");
    Intent startIntent1 = new Intent(context, AlarmReceiverFirst.class);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, startIntent1, PendingIntent.FLAG_CANCEL_CURRENT);

    Intent startIntent2 = new Intent(context, AlarmReceiverSecond.class);
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, startIntent2, PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 20);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Log.v("AlarmManager", "Starting AlarmManager for >= KITKAT version");
        alarm.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent1);
        alarm.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent2);
    } else {
        Log.v("AlarmManager", "Starting AlarmManager for < KITKAT version");
        alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent1);
        alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent2);
    }

    Log.v("AlarmManager", "AlarmManager has been started");
}

Create your first receiver class

public class AlarmReceiverFirst extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v(this.getClass().getSimpleName(), "first alarm receiver is called");
    }
}

Create your second receiver class

public class AlarmReceiverSecond extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v(this.getClass().getSimpleName(), "second alarm receiver is called");
    }
}

Register those receivers to your manifest

<receiver android:name=".AlarmReceiverFirst" />
<receiver android:name=".AlarmReceiverSecond" />

Not to be confused, what you called Id here is called request code. It is used for cancelling the pending intent.




回答2:


Intents are just the action PendingIntent is bound to execute once it triggers. But this triggering criteria are entirely depending on PendingIntent itself and RequestCode is playing here a pretty good role to uniquely identify, manage and trigger PendingIntent.

Therefore, no matter what the Intent is, if the requestCode is repeated then the latter PendingIntent will trigger. If you need to trigger multiple PendingIntents, the requestCode must be different from each other.




回答3:


You can have same name of intents but with different ids like following,

Intent i = new Intent(context, TriggerAlarm.class);  
PendingIntent pi =PendingIntent.getBroadcast(context,System.currentTimeMillis(),i,PendingIntent.FLAG_CANCEL_CURRENT);

And

Intent i = new Intent(context, TriggerNotification.class);
PendingIntent pi = PendingIntent.getBroadcast(context,System.currentTimeMillis(), i,PendingIntent.FLAG_CANCEL_CURRENT);

This way both the intents would be distinguished differently from each other and will get triggered.You can have any unique id instead of System.currentTimeMillis()



来源:https://stackoverflow.com/questions/38758424/pending-intent-with-different-intent-but-same-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!