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

时间:2018-12-20 10:58:41

标签: android android-anr-dialog nsd

我们的某些用户在启动时遇到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$33.createService (SystemServiceRegistry.java:573)
  at android.app.SystemServiceRegistry$33.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)

1 个答案:

答案 0 :(得分:0)

已解决。

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

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