2016-07-13 4 views
1

나는 내 질문에 대한 답변을 찾기 위해 어디에서나 검색했습니다. IbroadcastReceiver에서 여러 개의 알람 관리

내 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); 
}} 

답변

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

당신은 당신이 그들을 필요로 할 때 그렇지 않으면 존재하지 않을 것이다, 당신은 PendingIntent.getBroadcast()를 호출하기 전에 alertIntent에 엑스트라를 추가해야합니다.

0

감사 데이비드 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); 

감사합니다 :)