在broadcastReceiver中管理多个警报

时间:2016-07-13 18:11:49

标签: android notifications broadcastreceiver alarms

我到处寻找我的问题的答案。我

我在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);
}}

2 个答案:

答案 0 :(得分: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);

您需要在致电alertIntent 之前将额外内容添加到PendingIntent.getBroadcast() ,否则他们会在您需要时不在场。

答案 1 :(得分:0)

感谢David Wasser 如您所说,此代码可以使用附加功能

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

谢谢:)