我已經到處搜索以找到我的問題的答案。我在broadcastReceiver中管理多個警報
我在我的PreferenceActivity中創建了一個鬧鐘,它與我的廣播接收器一起顯示一個簡單的通知。但是,當我想要設置多個警報時,我不知道如何選擇要在broadcastReceiver上顯示的通知。
我用下面的代碼來設置報警:
public void SetAlarm(){
int id1=1, id2=2;
//Preferences
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(SettingActivity.this);
tA = prefs.getString("timePrefA_Key", "Default TimePrefrence");
tB = prefs.getString("timePrefB_Key", "Default TimePrefrence");
//Calendars
Calendar cA = Calendar.getInstance();
cA.setTimeInMillis(System.currentTimeMillis());
cA.set(Calendar.HOUR_OF_DAY, TimePreference.getHour(tA));
cA.set(Calendar.MINUTE, TimePreference.getMinute(tA));
cA.set(Calendar.SECOND,0);
Calendar cB = Calendar.getInstance();
cB.setTimeInMillis(System.currentTimeMillis());
cB.set(Calendar.HOUR_OF_DAY, TimePreference.getHour(tB));
cB.set(Calendar.MINUTE, TimePreference.getMinute(tB));
cB.set(Calendar.SECOND,0);
//Intents
Bundle bundle = new Bundle();
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alertIntent = new Intent(this,NotificationReceiver.class);
PendingIntent pi1 = PendingIntent.getBroadcast(this, id1,
alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
bundle.putInt("NotificationId1", id1);
PendingIntent pi2 = PendingIntent.getBroadcast(this, id2,
alertIntent, PendingIntent.FLAG_CANCEL_CURRENT);
bundle.putInt("NotificationId1", id2);
alertIntent.putExtras(bundle);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
am.setExact(AlarmManager.RTC_WAKEUP, cA.getTimeInMillis(), pi1);
am.setExact(AlarmManager.RTC_WAKEUP, cB.getTimeInMillis(), pi2);
}
else {
am.set(AlarmManager.RTC_WAKEUP, cA.getTimeInMillis(), pi1);
am.set(AlarmManager.RTC_WAKEUP, cB.getTimeInMillis(), pi2);
}}
這裏是我的廣播接收器。我的問題在Onreceive中。我應該如何選擇要顯示的通知?
public class NotificationReceiver extends BroadcastReceiver {
int NotifId1=1, NotifId2=2;
public void createnotification (Context context,String msg, String msgText, String msgAlert,Class <?> cls, int id){
PendingIntent notifIntent = PendingIntent.getActivity(context,0,
new Intent(context,cls),0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.produits);
mBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.produits));
mBuilder.setContentTitle(msg);
mBuilder.setTicker(msgAlert);
mBuilder.setContentText(msgText);
mBuilder.setContentIntent(notifIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
mBuilder.setWhen(System.currentTimeMillis());
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, mBuilder.build());
}
@Override
public void onReceive(Context context, Intent intent) {
Bundle extra = intent.getExtras();
if(extra != null) {
NotifId1 = extra.getInt("NotificationId1");
NotifId2 = extra.getInt("NotificationId2");
}
createnotification(context, "Petit déjeuner", "Heure du petit déjeuner", "Veuillez prendre votre petit déjeuner SVP", MainActivity.class,NotifId1);
createnotification(context, "Collation", "Heure de collation","Veuillez prendre votre collation SVP", MainActivity.class,NotifId2);
}}