What is the best way to get user location after each two minute even you app is killed
-
2create one background services and that service work in background itself. continuously get location.. – Viveka Patel Apr 07 '16 at 10:34
-
but when service is stop then who i can get location update – Nisar Ahmad Apr 07 '16 at 10:35
-
1Yes you can achieve this by putting some effort and R&D. – Aks4125 Apr 07 '16 at 10:36
-
i have don a lot r$d but fail that's why i have asked – Nisar Ahmad Apr 07 '16 at 10:37
-
You should use a Service for that, you cant do nothing if the user stops your service. – Nanoc Apr 07 '16 at 10:37
-
2Very easy to find out how to do this with a bit of googling – J Whitfield Apr 07 '16 at 10:37
-
@NisarAhmad you will find easily on Stack overflow or googling. – Aks4125 Apr 07 '16 at 10:38
-
@Aks4125 i have tried a lot but not the condition is avoid to use alarm manager – Nisar Ahmad Apr 07 '16 at 10:39
-
Try this link its best example [Link](http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/) if u cant understand let me know – Viveka Patel Apr 07 '16 at 10:41
-
@NisarAhmad [link 1](http://stackoverflow.com/questions/21726670/continuous-location-updates-in-background) [link 2](http://stackoverflow.com/questions/31978060/android-continuous-location-update) as well as @viveka's answer hint- use `AlarmManager` to achieve interval updates. – Aks4125 Apr 07 '16 at 10:45
-
Add a service and add a broadcast receiver for boot complete. So that service will run after restart also – Ameer Apr 07 '16 at 10:46
4 Answers
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.
-
but when app is killed or device reboot then service stops please tell me another way – Nisar Ahmad Apr 07 '16 at 10:43
-
-
@NisarAhmad http://developer.android.com/reference/android/app/Service.html#START_STICKY – sihao Apr 07 '16 at 10:49
-
1@Ameer Good suggestion, this will ensure that you can start your service once the phone restarts – sihao Apr 07 '16 at 10:50
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);

- 755
- 11
- 22
-
-
You can find samples at the links provided and try searching for answers to specific problems you encounter while trying to implement this yourself. – J Whitfield Apr 07 '16 at 10:44
-
I have added an example for the Timer. How to use the different types of services is in the started service link. I hope this answers your original question – J Whitfield Apr 07 '16 at 10:49
-
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.

- 316
- 1
- 2
- 9
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

- 1,152
- 16
- 17