0

I am trying to poll a device for long/lat every minute or so... I have been researching and found a few things. I was thinking of using:

PeriodicWorkRequestBuilder() - however, I have read that it can only execute every 15 minutes at a minimum...

What tech/design pattern are available to me, if I wanted to poll for data in a shorter period?

  • Are foreground services my only choice?
Bob
  • 295
  • 5
  • 19

1 Answers1

0

You can Use any Runnable but when app closed it will not working .

Foreground service is best choice but in android 9+, it will confuse the user

this is the link that show to you how use it:

Timer

BUT

I advice you to use an AlarmManager that work like work manager but it response better, you can Use Countdown timer for it, link below will help You to use it:

AlarmManager

Edited:

sample for open activity with alarm manager

Intent intent = new Intent(this, YOUR_MAIN_ACTIVITY.class);
intent.setAction(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, ID,
                    intent, 0);

final long DELAY_IN_MILLIS = DELAY_IN_MILLI_SECONDS+ 
System.currentTimeMillis();
AlarmManager alarmManager = (AlarmManager)
getSystemService(Activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, DELAY_IN_MILLIS,pendingIntent);

and there is a full example for you:

Example

alireza daryani
  • 787
  • 5
  • 16