通过 getSystemService 请求 NSD_SERVICE 时应用程序无响应

Application Not Responding when requesting NSD_SERVICE via getSystemService

我们的一些用户在启动时遇到 ANR。查看 Play 商店中的报告,它似乎发生在我们的应用程序调用时:

NsdManager nsdManager = (NsdManager)context.getSystemService(Context.NSD_SERVICE);

阅读文档我不明白这怎么会失败。

一旦进入此模式,应用程序将不会启动,直到手机重新启动。我只设法重现了一次问题,但在它发生时 Logcat 正在报告来自 NsdService

的错误
 E/NsdService: Failed to execute mdnssd [stop-discover, 0]
    com.android.server.NativeDaemonTimeoutException: command '709 mdnssd stop-discover 0' failed with 'null'
        at com.android.server.NativeDaemonConnector.executeForList(NativeDaemonConnector.java:490)
        at com.android.server.NativeDaemonConnector.execute(NativeDaemonConnector.java:401)
        at com.android.server.NativeDaemonConnector.execute(NativeDaemonConnector.java:396)
        at com.android.server.NsdService$DaemonConnection.execute(NsdService.java:698)
        at com.android.server.NsdService.stopServiceDiscovery(NsdService.java:743)
        at com.android.server.NsdService.-wrap7(Unknown Source:0)
        at com.android.server.NsdService$NsdStateMachine$EnabledState.processMessage(NsdService.java:305)
        at com.android.internal.util.StateMachine$SmHandler.processMsg(StateMachine.java:992)
        at com.android.internal.util.StateMachine$SmHandler.handleMessage(StateMachine.java:809)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.os.HandlerThread.run(HandlerThread.java:65)

这让我觉得 NSD 系统服务本身状态不佳。

我的问题是如何将 NsdService 置于这种状态,其次,我该如何恢复它?

Play 商店的报告仅针对 Android 8 和 8.1,如果有帮助的话,日志给出以下内容:

"main" prio=5 tid=1 Waiting
  | group="main" sCount=1 dsCount=0 flags=1 obj=0x74bcaed8 self=0x7b162c0a00
  | sysTid=6277 nice=0 cgrp=default sched=0/0 handle=0x7b1b9c99c8
  | state=S schedstat=( 90600226 20695151 166 ) utm=4 stm=4 core=5 HZ=100
  | stack=0x7fc55c0000-0x7fc55c2000 stackSize=8MB
  | held mutexes=
  at java.lang.Object.wait (Native method)
- waiting on <0x0aad0ffc> (a java.lang.Object)
  at java.lang.Thread.parkFor$ (Thread.java:2135)
- locked <0x0aad0ffc> (a java.lang.Object)
  at sun.misc.Unsafe.park (Unsafe.java:358)
  at java.util.concurrent.locks.LockSupport.park (LockSupport.java:190)
  at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt (AbstractQueuedSynchronizer.java:868)
  at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly (AbstractQueuedSynchronizer.java:1021)
  at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly (AbstractQueuedSynchronizer.java:1328)
  at java.util.concurrent.CountDownLatch.await (CountDownLatch.java:232)
  at android.net.nsd.NsdManager.init (NsdManager.java:477)
  at android.net.nsd.NsdManager.<init> (NsdManager.java:267)
  at android.app.SystemServiceRegistry.createService (SystemServiceRegistry.java:573)
  at android.app.SystemServiceRegistry.createService (SystemServiceRegistry.java:569)
  at android.app.SystemServiceRegistry$CachedServiceFetcher.getService (SystemServiceRegistry.java:1374)
- locked <0x0f76c685> (a java.lang.Object[])
  at android.app.SystemServiceRegistry.getSystemService (SystemServiceRegistry.java:1326)
  at android.app.ContextImpl.getSystemService (ContextImpl.java:1695)
  at android.content.ContextWrapper.getSystemService (ContextWrapper.java:727)

已解决。

使用 NsdService 的代码在 2 个活动之间共享。由于两者都需要在需要时确保它是 运行,因此这两个活动可能会尝试快速连续启动它。由于代码试图在启动服务之前 运行 停止服务,这导致快速启动-停止-启动,使 NsdService 处于中断状态(至少对于我们的应用程序而言)。

解决方案是引入一些状态以避免重新启动已经 运行 的服务。