Android的网络服务发现"超时"? [英] Android Network Service Discovery "timeout"?

查看:417
本文介绍了Android的网络服务发现"超时"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Android上网络服务查询(NSD)做广告设备上的REST服务器。我的REST服务器被包裹在 android.app.Service 的一个实例。

的onCreate ,我成功地启动了REST服务器和注册我的NSD服务。我可以看到我的网络中的其他设备上的NSD服务名称(如 我的服务)。

在我的的onDestroy ,我停止了REST服务器和注销NSD服务。

然而,似乎在发展,当我把我的应用程序(通过Eclipse)的一个新实例,也不能保证该服务的的onDestroy 将被调用。这意味着 我的服务仍然标榜,并在下一次服务启动时,我最终的我的服务(1) 我的服务(2)等。

据我所知,NSD将修改服务名称创建一个唯一的实例。我的问题是:

  1. 什么是对广告业务的预期超时被删除,如果他们不再存在?
  2. 有没有一种方法,以确保我的的onDestroy 当我把我的应用程序的新实例被称为?请问这种情况是类似于用户可能会遇到,如果他们安装我的应用程序的更新?即是的onDestroy 保证在运行中的应用程序被关闭,使应用程序的新版本,可以安装被称为?
  3. 有没有在我的应用程序的方式来检测旧的登记并删除它们?
解决方案

首先,有一个<一个href="https://$c$c.google.com/p/android/issues/detail?id=82744&q=nsdmanager&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars#makechanges"相对=nofollow>开放式问题跟踪这个问题上的AOSP问题跟踪的。谁提起了错误的人可以瑞普在所有的Andr​​oid 4.x的版本,但与5.x中没有测试

条款的简要说明:

  • NsdManager - 的类Android应用程序接口,登记他们的NSD服务,看看其它NSD服务。 NsdManager真的只是一个空壳,其建立与NsdService异步连接。
  • NsdService (com.android.server.NsdService) - 隐藏的,可以有多个客户端连接Android系统服务。是否所有繁重的任务和簿记用于创建和管理NSD服务和交流活动回客户端应用程序。
  • NSD服务 - 这NsdService创建您的名义做广告您的应用程序的presence在网络上的服务

我刚刚测试过这个问题5.1和问题是固定的(由它看起来像固定源进入5.0)。我创建了一个刚刚注册了一个NsdService和登录时的注意事项有什么事情发生,包括生命周期事件的onCreate,onResume,在onPause和的onDestroy一个简单的应用程序。我通过重新启动,启动我的应用程序,然后通过机器人工作室重新安装了以下日志。

  W / SimpleNsdActivity(14379)的onCreate
D / NsdService(3516):startMDnsDaemon
D / NsdService(3516):新客户端听了异步消息
D / NsdService(3516):新客户,渠道:com.android.internal.util.AsyncChannel
@ 3c114a11使者:android.os.Messenger@3213e776
D / NsdService(3516):注册服务
D / NsdService(3516):registerService:2名:NsdTest,类型:_http._tcp,主持人:
空,端口:1349
W / SimpleNsdActivity(14379):onResume
D / NsdService(3516):注册1 2
W / SimpleNsdActivity(14379):onServiceRegistered为NsdTest
D / NsdService(3516):SERVICE_REGISTERED原料:606 2NsdTest
D / NsdService(3516):客户端断开连接
D / NsdService(3516):终端客户ID 1全局ID 2型393225
D / NsdService(3516):unregisterService:2
D / NsdService(3516):stopMDnsDaemon
W / SimpleNsdActivity(15729)的onCreate
D / NsdService(3516):startMDnsDaemon
D / NsdService(3516):新客户端听了异步消息
D / NsdService(3516):新客户,渠道:com.android.internal.util.AsyncChannel
@ 38bace07使者:android.os.Messenger@26d8cd5d
D / NsdService(3516):注册服务
D / NsdService(3516):registerService:3名:NsdTest,类型:_http._tcp,主持人:
空,端口:1349
D / NsdService(3516):注册1 3
W / SimpleNsdActivity(15729):onResume
D / NsdService(3516):SERVICE_REGISTERED光地:606 3NsdTest
W / SimpleNsdActivity(15729):onServiceRegistered为NsdTest
 

您可以看到的是,无论的onPause()的onDestroy()被称为因此我的应用程序没有任何明确的清理的NSD服务。 NsdService被检测何时AsyncChannel到客户端被终止,并且适当地清洗它。您可以通过查看源<一拿到NsdService日志更深入的了解href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/5.1.0_r1/com/android/server/NsdService.java#163"相对=nofollow>这里。

总结这一切,我会从提示的问题作出答复:

  1. 对于4.x中没有超时,它会熬夜到重新启动。对于5.x的NsdService关闭NSD服务,当您的应用程序断开连接。
  2. 在没有办法保证的onDestroy()被调用时,一个更深层次的解释这里。通过Android的Studio安装查杀没有它运行的应用程序被调用。将是有趣的,如果通过App Store的更新行为方式相同或更多正常关闭正在运行的应用程序。
  3. 在5.x的不是一个问题,因为它杀死他们的时候了。在4.x中,有没有办法删除旧的注册,因为你立即失去了注册监听器。

I'm using Network Service Discover (NSD) on Android to advertise a REST server on the device. My REST server is wrapped in an instance of android.app.Service.

In the onCreate, I successfully start the REST server and register my NSD service. I can see the NSD service name on other devices within my network (e.g. My Service).

In my onDestroy, I stop the REST Server and unregister the NSD service.

However, it seems that while developing, when I push a new instance of my app (via Eclipse), there's no guarantee that the service's onDestroy will be called. This means that My Service is still advertised, and the next time the service starts, I end up with My Service (1), My Service (2), etc.

I understand that NSD will amend the service name to create a unique instance. My questions are:

  1. What's the expected timeout for advertised services to be removed if they no longer exist?
  2. Is there a way to ensure my onDestroy is being called when I push a new instance of my app? Would this circumstance be similar to what a user might experience if they install an update of my app? i.e. is the onDestroy guaranteed to be called when a running app is shutdown so a new version of that app can be installed?
  3. Is there a way within my app to detect the old registrations and remove them?

解决方案

First of all, there is an open issue tracking this problem on the AOSP issue tracker. The person who filed the bug could repro in all Android 4.x versions but did not test with 5.x.

A brief explanation of terms:

  • NsdManager - the class that Android applications interface with to register their NSD service and discover other NSD services. NsdManager is really just a shell that establishes an asynchronous connection with NsdService.
  • NsdService(com.android.server.NsdService) - hidden Android system service that can have connections from multiple clients. Does all the heavy lifting and book-keeping for creating and managing NSD service and communicating events back to client applications.
  • NSD service - the service that NsdService creates on your behalf that advertises your app's presence over the network.

I just tested this issue with 5.1 and the issue is fixed(from the source it looks like the fix went into 5.0). I created a simple application that just registers an NsdService and logs when anything of note happens, including lifecycle events onCreate, onResume, onPause, and onDestroy. I got the following logs by rebooting, starting my application, and then reinstalling via Android Studio.

W/SimpleNsdActivity(14379): onCreate
D/NsdService( 3516): startMDnsDaemon
D/NsdService( 3516): New client listening to asynchronous messages
D/NsdService( 3516): New client, channel: com.android.internal.util.AsyncChannel
@3c114a11 messenger: android.os.Messenger@3213e776
D/NsdService( 3516): Register service
D/NsdService( 3516): registerService: 2 name: NsdTest, type: _http._tcp., host:
null, port: 1349
W/SimpleNsdActivity(14379): onResume
D/NsdService( 3516): Register 1 2
W/SimpleNsdActivity(14379): onServiceRegistered as NsdTest
D/NsdService( 3516): SERVICE_REGISTERED Raw: 606 2 "NsdTest"
D/NsdService( 3516): Client disconnected
D/NsdService( 3516): Terminating client-ID 1 global-ID 2 type 393225
D/NsdService( 3516): unregisterService: 2
D/NsdService( 3516): stopMDnsDaemon
W/SimpleNsdActivity(15729): onCreate
D/NsdService( 3516): startMDnsDaemon
D/NsdService( 3516): New client listening to asynchronous messages
D/NsdService( 3516): New client, channel: com.android.internal.util.AsyncChannel
@38bace07 messenger: android.os.Messenger@26d8cd5d
D/NsdService( 3516): Register service
D/NsdService( 3516): registerService: 3 name: NsdTest, type: _http._tcp., host:
null, port: 1349
D/NsdService( 3516): Register 1 3
W/SimpleNsdActivity(15729): onResume
D/NsdService( 3516): SERVICE_REGISTERED Raw: 606 3 "NsdTest"
W/SimpleNsdActivity(15729): onServiceRegistered as NsdTest

You can see that neither onPause() nor onDestroy() were called and thus my application did no explicit cleanup for the NSD service. NsdService is detecting when the AsyncChannel to the client is terminated and is cleaning it up properly. You can get a deeper understanding of the NsdService logs by looking at the source here.

Summing it all up, I'll respond to the questions from the prompt:

  1. For 4.x there is no timeout, it will stay up until reboot. For 5.x NsdService shuts down the NSD service when your app disconnects.
  2. There is no way to guarantee onDestroy() is called, a deeper explanation here. Installation via Android Studio killing the running application without it being called. Would be interesting to see if updates via the App Store behave the same way or more gracefully shut down the running app.
  3. Not a problem in 5.x since it kills them right away. In 4.x, there's no way to remove old registrations since you immediately lose the registration listener.

这篇关于Android的网络服务发现&QUOT;超时&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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