0

I need some guideline on how to run a task every 1 minute, even though the app is not running. I need to read some data from a bluetooth device.

Thank you.

Marius Gheorghe
  • 607
  • 5
  • 7
  • Possible duplicate of [Service that runs every minute](https://stackoverflow.com/questions/21974587/service-that-runs-every-minute) – ʍѳђઽ૯ท Oct 17 '18 at 16:12

1 Answers1

2
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime() + 1000*60,
        1000*60, alarmIntent);

Create a broadcast receiver in given example AlarmReceiver.class and register this broadcast in manifest as well. Perform your task in onReceive() method is it will be called periodically.

Asfar Ali
  • 51
  • 4