0

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

fer
  • 57
  • 2
  • 9
  • have you solved it? – John Sardinha Jul 12 '16 at 18:10
  • no i still trying to do it – fer Jul 12 '16 at 19:59
  • Instead of using `setRepeating`, use only `set` and then in the broadcast onReceiver call the broadcast again – John Sardinha Jul 12 '16 at 20:07
  • public class AlarmReceiver extends BroadcastReceiver { private static final String TAG = "BANANEALARM"; Intent intent; PendingIntent pendingIntent; NotificationManager notificationManager; @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "BroadcastReceiver has received alarm intent."); Intent service1 = new Intent(context, AlarmService.class); String id = intent.getStringExtra("name"); service1.putExtra("name",id); context.startService(service1); } } – fer Jul 12 '16 at 20:11
  • That's nasty, I'll post an answer – John Sardinha Jul 12 '16 at 20:13

2 Answers2

0

Try using FLAG_UPDATE_CURRENT instead FLAG_ONE_SHOT.

Also make sure to reschedule the alarms after device reboot. Here is a good example.

Community
  • 1
  • 1
Georgy
  • 187
  • 12
0

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);`
John Sardinha
  • 3,079
  • 4
  • 21
  • 50