Android: Bonjour service stopped after the parent process crashed

My application is essentially a background service, which sometimes needs to register an NSD ( Bonjour ) service in order to enable socket server discovery, performed by the main background service (also executed by the application).

If I read the Android Bonjour Service doc correctly, then you start the Bonjour service (short for short):

 mNsdManager = Context.getSystemService(Context.NSD_SERVICE); mDiscoveryListener = new NsdManager.DiscoveryListener() mNsdManager.discoverServices( SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener); 

... and this is how you stop him:

 mNsdManager.unregisterService(mRegistrationListener); 

Here is the part that I can’t turn my head to: if the main service drops sharply, any Bonjour service that was registered during the failure continues to work, even if it no longer has a target (the socket server that helps to detect no longer exists) .

I cannot clean up the zombie Bonjour services when the main service restarts, since the mRegistrationListener service was originally registered, also no longer exists.

I suspect I'm taking the wrong approach: how can I make sure that I don't leave the Bonjour zombie service mess after the main service crashed?

+6
source share
2 answers

Android-specific Bonjour, you can try to deal with this failure by setting up your service as described in the answer here: Can I call the method before my application goes to crash

If you cannot set this to make the unregisterService call, you must set it to use the ActivityManager killBackgroundProcesses API . For this, you need to add permission for your manifest:

 android.permission.KILL_BACKGROUND_PROCESSES 
+1
source

If you correctly understood that the main service (the one that has the server socket) registers / unregisters the Nsd service, and the background service starts / stops the detection of the Nsd service. I think this is what you are doing, so your “approach” is correct.

Regarding the issue, I should welcome you to Android Nsd . There are many errors with the framework (among which you can find your problem) that with Android 6.0 have not been fixed, but instead, developers use other frameworks.

Returning to the problem, you can try UncaughtExceptionHandler , just keep in mind that all callbacks are called by the system asynchronously, and you can get NPE when it calls mRegistrationListener.onServiceUnregistered() , because, as you said, “this is not around” .

As for cleaning the service, this is theoretically possible, but only after the NsdManager configures the source code (you need to change the access modifier of several methods to achieve and then unregister mRegistrationListener from another process that would lead to its removal from the NsdManager listener NsdManager ). But this makes no sense if the application should be published on the market.

There is another workaround with which you can try / experiment. If I remember correctly (it may be wrong), the necessary cleaning occurs after disabling Nsd . I tried this through adb :

 // Disable adb shell service call servicediscovery 2 // Enable adb shell service call servicediscovery 2 i32 1 

However, note that making these calls programmatically cannot be trivial and most likely requires root, which again limits the audience of your application.

Regarding the killBackgroundProcesses() method proposed by @thril, it takes a string with the name of the application package as a parameter. But servicediscovery not an application, it is a system service. In addition, you can try to kill the process (although I don’t know which one) at runtime, but be careful, you should study what effect it brings to the system, and be sure that the service will be resumed as necessary ( automatically through the system or manually). Again, a root is needed for this.

Summing up the answer, before embarking on Nsd , I highly recommend that you search for its functionality / errors to avoid a possible waste of time and effort. Some links in addition to the link above:

PS Personally, I, after struggling with several errors in the Nsd structure, eventually wrote my own framework.

+2
source

All Articles