我有一个方法设置不同的挂起意图,通过意图将布尔值传递给广播接收器。但是在onRecieve方法中,我得到了相同的布尔值......
public static void setNotification(Context ctx) {
Calendar calendar = Calendar.getInstance();
cancelNotification(_instance);
Intent myIntent = new Intent(ctx, NotificationReceiver.class);
Intent dataIntent = new Intent(ctx, NotificationReceiver.class);
AlarmManager alarmManager = (AlarmManager) ctx
.getSystemService(ALARM_SERVICE);
if (!Prefs.getInstance().isLoggedIn) {
myIntent.putExtra("DATA", false);
pendingIntent = PendingIntent.getBroadcast(ctx, 0, myIntent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 2 * 60 * 1000, pendingIntent);
} else {
if (Prefs.getInstance().isDataReminder) {
dataIntent.putExtra("DATA", true);
dataPendingIntent = PendingIntent.getBroadcast(ctx, 0,
dataIntent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 3 * 60 * 1000,
dataPendingIntent);
}
if (Prefs.getInstance().reminderInterval.equals("1 Day")
|| Prefs.getInstance().reminderInterval.equals("")) {
myIntent.putExtra("DATA", false);
pendingIntent = PendingIntent.getBroadcast(ctx, 0, myIntent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1 * 60 * 1000,
pendingIntent);
} else {
myIntent.putExtra("DATA", false);
pendingIntent = PendingIntent.getBroadcast(ctx, 0, myIntent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 5 * 60 * 1000,
pendingIntent);
}
}
}
广播---
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
boolean isDataReminder = intent.getBooleanExtra("DATA", false);
Intent notificationService = new Intent(context,
NotificationService.class);
notificationService.putExtra("DATA", isDataReminder);
context.startService(notificationService);
}
不同的待处理意图是否可以传递不同的值,还是需要更改方法?
答案 0 :(得分:0)
为每个PendingIntent
使用不同的 requestCode 。 requestCode用于唯一标识挂起的意图。