2

I am developing an application in android studio, and I have to save the location of the device, I did a "locationService" class that extends service, so the location is segmented on my server and the service would be in the background. I have read the android documentation and there are two types of service (service and linked service), I use the linked service because I need to show the location data in an activity.

The problem is when I close the application, the service dies and does not record the change of location.

How can I prevent the linked service from dying. Thank you

David Wasser
  • 93,459
  • 16
  • 209
  • 274
Paul Quito
  • 45
  • 4

1 Answers1

0

I assume you mean a "bound" Service, not a "linked" Service. A bound Service will stop running when the last client disconnects from it. To prevent this, you need to call startService() and make sure that you return START_STICKY from onStartCommand() in your Service.

You don't need to use a "bound" Service in order to pass data from the Service to an Activity. Another way is to have your Service broadcast the data (by adding the data as an "extra" to an Intent and calling sendBroadcast() with the Intent). Your Activity can then set up a BroadcastReceiver to listen for any data braodcast by your Service.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thank you. The correct term is bundle, I already implemented it in my application and it worked correctly, now I want to implement another functionality, when in the background I can launch an activity and display the desired information, but when I minimize the application (press the home and I do not close it), and I send you new information and I want to put my application in the foreground showing that information. – Paul Quito Oct 03 '17 at 16:13
  • I assume you mean "bound", not "Bundle". When you want to bring your app to the foreground, just get a "launch Intent" for your app and call `startActivity()` with that. It will bring your app to the foreground so you can do what you want. See https://stackoverflow.com/a/14137045/769265 – David Wasser Oct 03 '17 at 16:26
  • Thank you. I have implemented this method and it has worked, now I have another problem, in my service I have a boolean variable that starts with a value of true, and from my application I command it to change to false, but when I close the application, something happens the service as a restart and my variable returns to the initial value (true). There is some way my service will not "reboot". – Paul Quito Oct 03 '17 at 20:44
  • Android can always kill the process hosting your `Service` and restart it. You cannot get around this. You need to come up with another method of dealing with this. You can store something in `SharedPreferences`, or some other persistent storage if necessary. – David Wasser Oct 03 '17 at 22:09
  • Thank you. I will try to find another solution to solve this, one last question, my application has a button with which I start the service and sends me to another activity, which happens if I press the back button and press the start button again my service ?. Do you create a new service like a thread? And if you create a new service how could you control that? – Paul Quito Oct 04 '17 at 02:37
  • Services are like singletons in Android. If you call startService() and the service is already running, Android doesn't start another instance. It just calls onStartCommand() on the existing instance – David Wasser Oct 04 '17 at 06:05
  • 1
    Thank you. I have been able to solve all the problems that have arisen during the development of the application. – Paul Quito Oct 04 '17 at 22:53