在 broadcastReceiver 中管理多个警报

Manage multiple alarms in the 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);
    }}

这是我的 BroadcastReceiver。我的问题在 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);
}}

此代码无效:

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

您需要在调用 PendingIntent.getBroadcast() 之前将 extras 添加到 alertIntent ,否则它们不会在您需要时出现。

谢谢大卫瓦瑟 如您所说,此代码具有附加功能

//AlarmManager
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    //Intent1
    Intent alertIntent1 = new Intent(this,NotificationReceiver.class);
    alertIntent1.putExtra("NotificationID", 1);
    PendingIntent pi1 = PendingIntent.getBroadcast(this, 1, alertIntent1,
            PendingIntent.FLAG_UPDATE_CURRENT);
    //Set Alarms
    am.setRepeating(AlarmManager.RTC_WAKEUP, cA.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi1);

谢谢:)