具有不同意图但ID相同的待定意图 [英] Pending intent with different intent but same ID
问题描述
我有两个待与警报管理器一起使用的待定意图,一个是:
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);
,另一个是:
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
我的问题是:
这些待处理的Intent是否彼此不同?因为意图不同,但ID相同
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?
推荐答案
因此,简单的方法是直接自己进行测试。
我已经在计算机上对其进行了测试,这是我得到的:
So the easy way is test it directly by yourself. I have tested it on my computer and here is what i got:
这些待处理的Intent是否彼此不同?因为意图不同但ID相同
Are these pendingIntents different from each other?? because the intents are different but the Ids are same
-是的,尽管ID相同,但它们彼此不同
如果我为这些未决的意图中的每一个设置警报管理器,它们都是触发还是替换另一个?
If I set alarm manager for each of these pending intent do both of them trigger or one replace another?
-两者其中的
这是我的测试代码,您可以自己复制并尝试
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");
}
}
创建第二个接收器类
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" />
不要混淆,您在此处称为ID的称为请求代码。用于取消待处理的意图。
Not to be confused, what you called Id here is called request code. It is used for cancelling the pending intent.
这篇关于具有不同意图但ID相同的待定意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!