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.
You should use
ForegroundServiceinstead ofServiceand callstartForegroundService()when you want to start it.Next in this new
ForegroundServicein onCreate method callstartForeground(). WithstartForegroundyou have to pass anotificationto inform the user that service isrunning.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.