安卓:停止一个Bonjour服务留有运行后,父进程退出abrubtly [英] Android : Stopping a Bonjour service left running after the parent process quit abrubtly

查看:614
本文介绍了安卓:停止一个Bonjour服务留有运行后,父进程退出abrubtly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序本质上是需要偶尔注册了一个 NSD 服务(卓悦服务)后台服务目的使由主后台服务(又名由应用程序运行),运行套接字服务器的发现。

My app is essentially a background service that needs to occasionally register an NSD service (Bonjour service) for the purpose of enabling discovery of a socket server run by the main background service (aka run by the app).

如果我读了 Android的Bonjour服务DOC 正确,你这是怎么启动卓悦服务(简称为简要):

If I am reading the Android Bonjour Service doc correctly, this is how you start the Bonjour service (abbreviated for conciseness):

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 it:

mNsdManager.unregisterService(mRegistrationListener);

下面的部分,我不能绕到我的头:如果主服务出现突然下降,这是在崩溃的时间内注册的卓悦服务继续运行即使它不再有目的(socket服务器可以帮助发现不再是左右)。

Here the part I can't wrap my head around: if the main service goes down abruptly, any Bonjour service that was registered at the time of the crash keeps running even though it no longer has a purpose (the socket server it helps discover is no longer around).

我可以在重新启动时不清理僵尸卓悦服务的主要服务,因为 mRegistrationListener 服务最初与注册也不再左右。

I can't event cleanup the zombie Bonjour services when the main service is restarted because the mRegistrationListener the service was initially registered with is also no longer around.

我怀疑我采取了错误的做法:我如何确保我不离开僵尸乱七八糟的卓悦后,背后的主要服务坠毁服务?

I suspect I am taking the wrong approach: how do I make sure I don't leave a mess of zombie Bonjour services behind after the main service crashed?

推荐答案

非特异性到Android卓悦,你可以尝试通过在这里的答案是概述设置您的服务来处理崩溃:<一href=\"http://stackoverflow.com/questions/19945407/can-i-call-a-method-before-my-application-go-to-crash\">Can我把我的应用程序之前的方法去崩溃

Non-specific to Android Bonjour, you could try to handle the crash by setting up your service as is outlined in the answer here: Can I call a method before my application go to crash

如果您不能设置这使unregisterService电话,你应该能够将其设置为使用ActivityManager的API <一个href=\"http://developer.android.com/reference/android/app/ActivityManager.html#killBackgroundProcesses(java.lang.String)\"相对=nofollow> killBackgroundProcesses 。这就需要添加权限您的清单:

If you can't set this up to make the unregisterService call, you should be able to set it up to use ActivityManager's API killBackgroundProcesses. This requires adding the permission to your manifest:

android.permission.KILL_BACKGROUND_PROCESSES

这篇关于安卓:停止一个Bonjour服务留有运行后,父进程退出abrubtly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆