AlarmManager does not repeat

I encode a “simple” notifier, which consists of calling the website, checking the response, and notifying if something new.

I use the service to perform http operations, and I would like AlarmManager to redial the service call at a given frequency. I checked tutorials such as this and other examples, and since I want the service to be scheduled either whenever the user leaves the settings screen (only the activity that it has been so far) and after the BOOT is completed, so I created class for transferring the planning code.

public class Scheduler { public static boolean cancelScheduledService(Context ctx, Intent serviceIntent) { boolean success = true; Log.v("novUmbria", "Scheduler.cancelScheduledService"); try { AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE); am.cancel( PendingIntent.getBroadcast( ctx, 0, new Intent(ctx, NotificadorService.class), // si ya existe, no genera un 2º PendingIntent.FLAG_CANCEL_CURRENT ) ); Log.v("novUmbria", "Scheduler.cancelScheduledService Servicio cancelado"); } catch (Exception e) { Log.e("novUmbria", "Scheduler.cancelScheduledService Excepción: " + e.getMessage()); success = false; } return success; } public static boolean scheduleService(Context ctx, Intent serviceIntent, long interval) { boolean success = true; Log.v("novUmbria", "Scheduler.scheduleService Servicio "); try { Calendar cal = Calendar.getInstance(); SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm"); // timeformat.setTimeZone(TimeZone.getTimeZone("Europe/Madrid")); AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE); am.setRepeating( // am.setInexactRepeating( // AlarmManager.ELAPSED_REALTIME_WAKEUP, AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), interval, PendingIntent.getBroadcast( ctx, 0, new Intent(ctx, NotificadorService.class), // si ya existe, no genera un 2º PendingIntent.FLAG_CANCEL_CURRENT ) ); Log.v("novUmbria", "Scheduler.scheduleService Servicio programado a las " + timeformat.format(cal.getTime()) + " cada " + (interval / 60000) + " minutos" ); startService(ctx, serviceIntent); Log.v("novUmbria", "Scheduler.scheduleService Servicio iniciado" ); } catch (Exception e) { Log.e("novUmbria", "Scheduler.scheduleService Excepción: " + e.getMessage()); success = false; } return success; } public static boolean startService(Context ctx, Intent serviceIntent) { boolean success = true; Log.v("novUmbria", "Scheduler.startService"); try { ctx.startService(serviceIntent); Log.v("novUmbria", "Scheduler.startService Servicio iniciado"); } catch (Exception e) { Log.e("novUmbria", "Scheduler.startService Excepción: " + e.getMessage()); success = false; } return success; } 

}

Here's a call to the scheduler from the Activity settings

 //Settings Activity @Override protected void onStop() { Log.v("novUmbria", "SettingsActivity.onStop"); Intent serviceIntent = new Intent(getApplicationContext(), NotificadorService.class); Scheduler.cancelScheduledService(getApplicationContext(), serviceIntent); long frequency = 1000 * 60 / 2; Scheduler.scheduleService(getApplicationContext(), serviceIntent, frequency ); Log.v("novUmbria", "SettingsActivity.onStop scheduleService"); super.onStop(); } 

Thing is: logcat tells me that the service is getting scheduled (or rather, that it does not throw an exception), and it starts the first time. But after that, no matter the long or short interval, it never repeats. I tried several flags RTC, RTC_WAKEUP, ELAPSED_REALTIME, etc., but I did not get anything.

My test device is completely updated by Nexus 4. I even rebooted it, so I checked that the BOOT_COMPLETE receiver is working fine, but it never repeats the service calls.

Any ideas on where the problem is?

Thanks in advance.

+4
source share
1 answer

I found the answer here .

Apparently, if you want to schedule a SERVICE, you are not using PendingIntent.getBroadcast , but PendingIntent.getService .

It's just a small change, and it repeats as it should.

Hope this helps someone else :)

+6
source

Source: https://habr.com/ru/post/1500175/


All Articles