0

The Main aim of my application is to trace the path on the map in realtime which the user traverses regardless of the fact that the user has opened the application or not.

With the help of a service I am able to request the location updates i.e. the current location of the user and broadcast them to my MainActivity to trace the path accordingly.

1 Answers1

0

You should use ForegroundService instead of Serviceand call startForegroundService() when you want to start it.

Next in this new ForegroundService in onCreate method call startForeground(). With startForeground you have to pass a notification to inform the user that service is running.

Another problem is that many devices use different kind of tricks to protect battery and try to kill working services.

To track location for a long time You should try to get users permission to disable battery optimization.

MarkWalczak
  • 1,532
  • 3
  • 16
  • 24
  • I did as instructed regarding the ForegroundService but it shows the following error : Bad notification for startForeground – Robin Singh Jun 27 '19 at 13:17
  • Did you use channel? Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel. https://developer.android.com/training/notify-user/channels – MarkWalczak Jun 27 '19 at 13:20
  • Go through following link where you can find a solution to the crash you are facing. https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1 – Vijay Patole Jun 27 '19 at 13:24
  • Wasn't doing this earlier but yeah i did use a channel now, The Notification works fine but the issue stills remains unsolved. I still cant trace the path in background. – Robin Singh Jun 27 '19 at 13:27
  • What device are you using? – MarkWalczak Jun 27 '19 at 13:28
  • You can also try to use broadcast receiver to restart the service when it's get killed. https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android – MarkWalczak Jun 27 '19 at 13:35
  • I am using POCO F1 and to mimic the movement of the user i am using a third Party Application to obtain a Mock Location. I am not using the Broadcast Receiver to restart the application,For that i have stored a boolean in the Shared Preferences so whenever my application is killed, i check the Status of the service. – Robin Singh Jun 27 '19 at 13:39
  • I forgot to mention that you need – MarkWalczak Jun 27 '19 at 13:52
  • Did you try to use your app on emulator? – MarkWalczak Jun 27 '19 at 13:52
  • idk if it will be working with the Foreground Service, but it sure is now working with the normal service with just one limitation. I can successfully trace the user traversed path ONLY the app is in background. For the time being, I am satisfied with this. – Robin Singh Jun 28 '19 at 11:26