Android: Have a job to check url

Asked

Viewed 22 times

1

I need to have a job for an app of mine that runs every day to check if a URL is returning correct data. In case I don’t return it, I should get an e-mail. Does anyone have any idea how to do this?

I know I can check if the URL works by Junit, but my problem is programming this execution every day on Android.

  • 1

    I strongly recommend taking a look at the Job Scheduler API or similar.

1 answer

0

To schedule tasks that will be performed over and over again in the future, you can use the mechanism of Alarms android.

Depending on your use case the parameters and the alarm type can change, but it’s 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.