-1

I need to run a code in an Android App every 5 minutes even if the app is in background. I dont know much about android studio, like where to put the code, and how to properly do it to avoid crashes and diferent errors when is in the background.

The app is a webview that extract and Id from the JS from the site code and then I need to run a script with that ID every 5 minutes.

Zoe
  • 27,060
  • 21
  • 118
  • 148
pimienta
  • 55
  • 1
  • 8
  • Take a look at this : https://developer.android.com/reference/android/app/job/JobScheduler.html – diegoveloper Dec 18 '17 at 16:18
  • 2
    Java != JavaScript. And then: there are no detours. You do such things ... by spending the time to research and understand them. – GhostCat Dec 18 '17 at 16:21

1 Answers1

-1

You can use AlarmManager and BroadcastReceiver to get this job done.

Intent intent = new Intent(this, YourBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*60*1000, pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();`
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64