N
N
Nikolarsen2018-02-06 08:29:50
Java
Nikolarsen, 2018-02-06 08:29:50

How to set alarm multiple times (dynamically)?

There is an activity with this code...

@Override
  protected void onStart(){
    super.onStart();
  }
  
  public void onetimeTimer(View view){
    
    openTimePickerDialog(true);
  
  }
  
  private void openTimePickerDialog(boolean is24r){
    Calendar calendar = Calendar.getInstance();

      timePickerDialog = new TimePickerDialog(
      ConfigActivity.this, 
      onTimeSetListener, 
      calendar.get(Calendar.HOUR_OF_DAY), 
      calendar.get(Calendar.MINUTE), 
      is24r);
    timePickerDialog.setTitle("Set Alarm Time");  

    timePickerDialog.show();

  }

    OnTimeSetListener onTimeSetListener
    = new OnTimeSetListener(){

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

      Calendar calNow = Calendar.getInstance();
      Calendar calSet = (Calendar) calNow.clone();

      calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
      calSet.set(Calendar.MINUTE, minute);
      calSet.set(Calendar.SECOND, 0);
      calSet.set(Calendar.MILLISECOND, 0);

      if(calSet.compareTo(calNow) <= 0){
        //Today Set time passed, count to tomorrow
        calSet.add(Calendar.DATE, 1);
      }

      setAlarm(calSet);
    }};

  private void setAlarm(Calendar targetCal){

  
    Intent intent = new Intent(getBaseContext(), AlarmManagerBroadcastReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1 , intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    //alarmManager.cancel(pendingIntent);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

  }

The alarm clock is set once. That is, I bet at 9:00 and then again at 10:30 for example. Only the last one works. It is necessary that both work... How to achieve such a result?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2018-02-06
@onepavel

a similar question has already been asked on so
You need to use different Broadcast id's for the pending intents
https://stackoverflow.com/questions/8469705/how-to...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question