Why is my service created several times?

IIUC, there should be only one instance of this Android service, this is singleton.

However, my service receives multiple instances, although I do nothing for this.

If the service fails (for example, when uninstalling the application via adb), it receives the scheduled reboot ("Planning the restart of the emergency service .."). I understand that this is the effect of using sticky services.

After that, when my application starts, it calls startService () and bindService (), and the service gets the proper start and binding. But the service is then restantiated and onCreate () is called multiple times, how many times has it been scheduled for a reboot.

Each instance then waits for clients to contact and register, but onBind () is only called in the "main" instance of the service. Additional instances wait a bit for the client to bind, and since this does not happen, they call stopSelf ().

But stopSelf () has absolutely no effect in these "dead" cases, onDestroy () is never called.

The "main" instance of the service works as expected, and when it decides to call stopSelf (), onDestroy () is really called.

Worse, all these dead cases accumulate; they are never destroyed. Therefore, their only possible end is a crash (which happens every time I start / install via adb), and thus a scheduled restart.

So in the end, I get a lot of these dead cases that restart gradually about once per minute.

Does anyone know what is going on?

+6
android
source share
4 answers

I have the same behavior if I use eclipse to restart the application with the remote service. According to logcat, the system believes that the killed service crashed and tried to restart the service. At the same time, the service restarted with a restarted application. For some unknown reason, the Android system does not understand that a running service already exists, and is trying to start a new one.

This happens several times on Optimus one, Galaxy tab and EVO 3D. This is normal with the Nexus.

+2
source share

Because I have not seen your code, this is just an assumption: you may have a memory leak that prevents the service from breaking correctly. This is the only reason I could come up with multiple instances of the service. For example, if a service is serving some object that also has a link to your service. This happens with inner classes.

Check out this video from Google I / O to see if this issue is at your service and how to find it: http://www.youtube.com/watch?v=_CruQY55HOk&feature=player_embedded

+1
source share

if you use a section for an exception in onstart() . if ur started the service using the onclick button or, for example, clicking on the icon several times means what it will do, if the service is already running, it will go to onstart (), so the method will be launched again and again the service several times ... . Ur method has been working for several times, this I said, to decorate my hunch maybe the exact code will be Explaind properlly

0
source share

if your application crashes or kills the process to which it belongs, for example, System.exit (), it will start after the exit or launch of your application, if your service works in the same process with the application. Because you kill the process, and Android discovers that your service should not stop, so Android will restart it for you after your application exits. And why the service starts again after restarting the application, I think this is an Android error, it redistributes the new process in your application, and does not use the process allocated to your service.

So how to solve this problem? just set the android: process = ": background" attribute (everything you want here starts with :) to your node service in AndroidManifest.xml. Hope this helps you.

-one
source share

All Articles