Android: Have a job to check url

1

I need to have a job for my app that runs every day to check if a URL is returning correct data. If you do not return, you should get an email to let me know. Does anyone have any idea how this is done?

I know I can check if the URL works for JUnit, but my problem is to schedule this run every day on Android.

    
asked by anonymous 11.08.2017 / 10:01

1 answer

0

To schedule tasks that will run repeatedly in the future, you can use the Alarms engine in> of Android.

Depending on your use case the parameters and alarm type may change, but is something like:

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

// Intent para invocar uma classe no horário agendado
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

// Cria um calendário e muda sua hora para 14:00
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);

// setInexactRepeating() requer um dos intervalos definidos nas constantes do AlarmManager
// Neste exemplo o intervalo é AlarmManager.INTERVAL_DAY (um dia)
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);

In this example an alarm was created for 14h of the current day, with an inaccurate daily repetition.

    
11.08.2017 / 14:52