-1

What is the best way to get user location after each two minute even you app is killed

Dharman
  • 30,962
  • 25
  • 85
  • 135
Nisar Ahmad
  • 170
  • 1
  • 7

4 Answers4

0

You can choose to use a Background Service for your activity.

Within this service, you set your service to poll for your location at the interval you wish.

After which, you can use LocalBroadcastManager to broadcast the result of your interest to return the useful results to your application.

Dharman
  • 30,962
  • 25
  • 85
  • 135
sihao
  • 431
  • 2
  • 11
0

Use a Started service to run code in the background even when the app is killed. Use a Timer and Timer task to run your location gathering code at specified intervals.

Timer example

new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {}
        }, 0, 1000);
J Whitfield
  • 755
  • 11
  • 22
0

Apart from what J Whitfield and sihao have suggested, You can also make your service as a Start service (not an Intent Service). Then on the onStartCommand of service, return START_STICKY.

You can also try to run your service in separate process so it won't be killed even if your app is killed.

am110787
  • 316
  • 1
  • 2
  • 9
0

Background Service Example Android

Use a Android Service to run code in the background even when the app is killed. Use a BroadcasteReceiver to restart the service when app is killed by user and Android JobService to run your location gathering code at specified intervals.

Try This its working for me getting the location when app is in killed state

Android-Background-Services-Exmaple

https://github.com/surenderkhowal/Android-Background-Services-Exmaple

Surender Kumar
  • 1,152
  • 16
  • 17