androidandroid-studioalarmmanageralarms

Android alarmmanager set alarms in specific days and repeat them all the weeks


i making a app where you can set alarms . it actually works but just one time. and i need to do more than once .im trying to do an class schedule alarms.

for example i have class mondays at seven , so i need to start the alarm every monday. but also i have another class tuesday and have to do it the same.

here is my code that works pd-> rqs1 = 1

Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        cal.set(Calendar.HOUR_OF_DAY, horai); // i put the hour with the interface
        cal.set(Calendar.MINUTE,minutoi);///
       cal.set(Calendar.DAY_OF_WEEK,dias.getSelectedItemPosition()+1);
        cal.add(Calendar.SECOND, 2);



        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        intent.putExtra("name", curso); // i put the name of the curso
      PendingIntent pendingIntent =
               PendingIntent.getBroadcast(getBaseContext(),
                      RQS_1, intent, PendingIntent.FLAG_ONE_SHOT);


    AlarmManager alarmManager =
            (AlarmManager)getSystemService(Context.ALARM_SERVICE);


    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            cal.getTimeInMillis(), 24 * 7 * 60 * 60 * 1000 , pendingIntent);`

so, i have been looking how to do it. please help me . Thanks


Solution

  • 1- Replace this

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            cal.getTimeInMillis(), 24 * 7 * 60 * 60 * 1000 , pendingIntent);`
    

    with this:

    alarmManager.set(AlarmManager.RTC_WAKEUP,
                cal.getTimeInMillis(), 24 * 7 * 60 * 60 * 1000 , pendingIntent);
    

    2- Replace this

    PendingIntent pendingIntent =
               PendingIntent.getBroadcast(getBaseContext(),
                      RQS_1, intent, PendingIntent.FLAG_ONE_SHOT);
    

    with this:

    PendingIntent pendingIntent =
               PendingIntent.getBroadcast(getBaseContext(),
                      RQS_1, intent, PendingIntent.FLAG_UPDATE_CURRENT );
    

    3- Add this inside onReceive:

      Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        intent.putExtra("name", curso); // i put the name of the curso
      PendingIntent pendingIntent =
               PendingIntent.getBroadcast(getBaseContext(),
                      RQS_1, intent, PendingIntent.FLAG_UPDATE_CURRENT );
    
    
    AlarmManager alarmManager =
            (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    
    
    alarmManager.set(AlarmManager.RTC_WAKEUP,
            cal.getTimeInMillis(), 24 * 7 * 60 * 60 * 1000 , pendingIntent);`