我正在使用Android中PAHO库的MQTT客户端实现。有时,应用程序在尝试发布消息时会锁定(我认为)并且我无法弄清楚发生了什么。
我也提前为所有长篇大论道歉。我真的不知道重要的是什么,SO“扰流”标签>!
无法隐藏细节。
MQTT客户端正在一个单独的线程中运行,但我正在从UI线程调用mqtt客户端实例。也许这导致锁定?我应该将客户端定义为线程的一个实例,并在线程中调用一个方法将该操作转发给客户端吗?
当应用程序挂起时,一段时间ANR开始并要求关闭应用程序。当我这样做时,我从/data/anr/traces.txt获取了我的进程的以下堆栈跟踪:
DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0)
"main" prio=5 tid=1 WAIT
| group="main" sCount=1 dsCount=0 obj=0x40a59460 self=0x109d828
| sysTid=22050 nice=0 sched=0/0 cgrp=default handle=1074492552
| schedstat=( 8524633000 459501000 2204 ) utm=827 stm=25 core=1
at java.lang.Object.wait(Native Method)
- waiting on <0x410f4bd0> (a java.lang.Object)
at java.lang.Object.wait(Object.java:364)
at org.eclipse.paho.client.mqttv3.internal.MqttDeliveryTokenImpl.waitUntilSent(MqttDeliveryTokenImpl.java:120)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.internalSend(ClientComms.java:85)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:118)
at org.eclipse.paho.client.mqttv3.MqttTopic.publish(MqttTopic.java:62)
at se.chalmers.pd.dashboard.MqttWorker.publish(MqttWorker.java:177)
at se.chalmers.pd.dashboard.ApplicationController.sendResponse(ApplicationController.java:238)
at se.chalmers.pd.dashboard.ApplicationController.onLoadComplete(ApplicationController.java:358)
at se.chalmers.pd.dashboard.MainActivity$CustomWebViewClient.onPageFinished(MainActivity.java:89)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:275)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
"Thread-3645" prio=5 tid=12 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x4111a5f8 self=0x13cc698
| sysTid=22083 nice=0 sched=0/0 cgrp=default handle=19370528
| schedstat=( 35731000 17350000 70 ) utm=1 stm=2 core=0
at dalvik.system.NativeStart.run(Native Method)
"MQTT Client Callback" prio=5 tid=17 WAIT
| group="main" sCount=1 dsCount=0 obj=0x41118bf0 self=0x13bb500
| sysTid=22082 nice=0 sched=0/0 cgrp=default handle=20691280
| schedstat=( 392860000 110613000 493 ) utm=35 stm=4 core=1
at java.lang.Object.wait(Native Method)
- waiting on <0x410d1c48> (a java.lang.Object)
at java.lang.Object.wait(Object.java:364)
at org.eclipse.paho.client.mqttv3.internal.MqttDeliveryTokenImpl.waitUntilSent(MqttDeliveryTokenImpl.java:120)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.internalSend(ClientComms.java:85)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:118)
at org.eclipse.paho.client.mqttv3.MqttTopic.publish(MqttTopic.java:62)
at se.chalmers.pd.dashboard.MqttWorker.publish(MqttWorker.java:177)
at se.chalmers.pd.dashboard.ApplicationController.sendResponse(ApplicationController.java:238)
at se.chalmers.pd.dashboard.ApplicationController.handleSystemMessage(ApplicationController.java:210)
at se.chalmers.pd.dashboard.ApplicationController.onMessage(ApplicationController.java:154)
at se.chalmers.pd.dashboard.MqttWorker$1$CustomMqttCallback.notifyCallbacks(MqttWorker.java:133)
at se.chalmers.pd.dashboard.MqttWorker$1$CustomMqttCallback.messageArrived(MqttWorker.java:128)
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:248)
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:139)
at java.lang.Thread.run(Thread.java:856)
"MQTT Client Comms Sender" prio=5 tid=16 TIMED_WAIT
| group="main" sCount=1 dsCount=0 obj=0x41118ae0 self=0x13bada0
| sysTid=22081 nice=0 sched=0/0 cgrp=default handle=20689392
| schedstat=( 6038000 11147000 189 ) utm=0 stm=0 core=0
at java.lang.Object.wait(Native Method)
- waiting on <0x410ffd90> (a java.lang.Object)
at java.lang.Object.wait(Object.java:401)
at org.eclipse.paho.client.mqttv3.internal.ClientState.get(ClientState.java:555)
at org.eclipse.paho.client.mqttv3.internal.CommsSender.run(CommsSender.java:83)
at java.lang.Thread.run(Thread.java:856)
"MQTT Client Comms Receiver" prio=5 tid=15 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x41116048 self=0x13ba870
| sysTid=22080 nice=0 sched=0/0 cgrp=default handle=20619056
| schedstat=( 656144000 184252000 2369 ) utm=59 stm=6 core=1
at libcore.io.Posix.recvfromBytes(Native Method)
at libcore.io.Posix.recvfrom(Posix.java:131)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
at libcore.io.IoBridge.recvfrom(IoBridge.java:503)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
at libcore.io.Streams.readSingleByte(Streams.java:41)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:236)
at java.io.DataInputStream.readByte(DataInputStream.java:96)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:51)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:86)
at java.lang.Thread.run(Thread.java:856)
"CookieSyncManager" prio=5 tid=14 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x410d0ad0 self=0x1286e20
| sysTid=22066 nice=10 sched=0/0 cgrp=bg_non_interactive handle=19445224
| schedstat=( 562000 10095000 5 ) utm=0 stm=0 core=0
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:118)
at android.os.Looper.loop(Looper.java:118)
at android.webkit.WebSyncManager.run(WebSyncManager.java:90)
at android.webkit.CookieSyncManager.run(CookieSyncManager.java:61)
at java.lang.Thread.run(Thread.java:856)
"AsyncTask #1" prio=5 tid=13 WAIT
| group="main" sCount=1 dsCount=0 obj=0x410d0920 self=0x121f5b0
| sysTid=22065 nice=10 sched=0/0 cgrp=bg_non_interactive handle=19335920
| schedstat=( 5753000 108824000 12 ) utm=0 stm=0 core=0
at java.lang.Object.wait(Native Method)
- waiting on <0x410d0ab8> (a java.lang.VMThread) held by tid=13 (AsyncTask #1)
at java.lang.Thread.parkFor(Thread.java:1231)
at sun.misc.Unsafe.park(Unsafe.java:323)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:157)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2022)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:413)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1009)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1069)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
"WebViewCoreThread" prio=5 tid=11 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x410caa38 self=0x1266550
| sysTid=22063 nice=0 sched=0/0 cgrp=default handle=18808064
| schedstat=( 273871000 128721000 264 ) utm=20 stm=7 core=0
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:118)
at android.os.Looper.loop(Looper.java:118)
at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:728)
at java.lang.Thread.run(Thread.java:856)
"Binder Thread #2" prio=5 tid=10 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x410c1ef0 self=0x129de58
| sysTid=22062 nice=0 sched=0/0 cgrp=default handle=19025912
| schedstat=( 1983000 23992000 8 ) utm=0 stm=0 core=1
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #1" prio=5 tid=9 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x410ba370 self=0x127cc90
| sysTid=22061 nice=0 sched=0/0 cgrp=default handle=18949528
| schedstat=( 2932000 32665000 18 ) utm=0 stm=0 core=0
at dalvik.system.NativeStart.run(Native Method)
"FinalizerWatchdogDaemon" daemon prio=5 tid=8 TIMED_WAIT
| group="main" sCount=1 dsCount=0 obj=0x410b6320 self=0x1227790
| sysTid=22059 nice=0 sched=0/0 cgrp=default handle=19405328
| schedstat=( 3721000 6943000 27 ) utm=0 stm=0 core=0
at java.lang.VMThread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:1031)
at java.lang.Thread.sleep(Thread.java:1013)
at java.lang.Daemons$FinalizerWatchdogDaemon.run(Daemons.java:213)
at java.lang.Thread.run(Thread.java:856)
"FinalizerDaemon" daemon prio=5 tid=7 WAIT
| group="main" sCount=1 dsCount=0 obj=0x410b61c8 self=0x12207b8
| sysTid=22058 nice=0 sched=0/0 cgrp=default handle=19405208
| schedstat=( 1055000 601000 7 ) utm=0 stm=0 core=1
at java.lang.Object.wait(Native Method)
- waiting on <0x40a4f5d0> (a java.lang.ref.ReferenceQueue)
at java.lang.Object.wait(Object.java:401)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:102)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:73)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
at java.lang.Thread.run(Thread.java:856)
"ReferenceQueueDaemon" daemon prio=5 tid=6 WAIT
| group="main" sCount=1 dsCount=0 obj=0x410b6060 self=0x129d168
| sysTid=22057 nice=0 sched=0/0 cgrp=default handle=19404976
| schedstat=( 573000 395000 11 ) utm=0 stm=0 core=1
at java.lang.Object.wait(Native Method)
- waiting on <0x40a4f4f8>
at java.lang.Object.wait(Object.java:364)
at java.lang.Daemons$ReferenceQueueDaemon.run(Daemons.java:128)
at java.lang.Thread.run(Thread.java:856)
"Compiler" daemon prio=5 tid=5 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x410b5f70 self=0x127c4d0
| sysTid=22056 nice=0 sched=0/0 cgrp=default handle=19172728
| schedstat=( 151015000 59330000 862 ) utm=9 stm=6 core=0
at dalvik.system.NativeStart.run(Native Method)
"JDWP" daemon prio=5 tid=4 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x410b5e88 self=0x12b39c8
| sysTid=22055 nice=0 sched=0/0 cgrp=default handle=19323736
| schedstat=( 4263000 2800000 18 ) utm=0 stm=0 core=1
at dalvik.system.NativeStart.run(Native Method)
"Signal Catcher" daemon prio=5 tid=3 RUNNABLE
| group="system" sCount=0 dsCount=0 obj=0x410b5d90 self=0x1268fd8
| sysTid=22054 nice=0 sched=0/0 cgrp=default handle=19014912
| schedstat=( 3760000 203000 4 ) utm=0 stm=0 core=1
at dalvik.system.NativeStart.run(Native Method)
"GC" daemon prio=5 tid=2 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x410b5cb0 self=0x12257d8
| sysTid=22053 nice=0 sched=0/0 cgrp=default handle=19042944
| schedstat=( 271726000 42594000 230 ) utm=26 stm=1 core=1
at dalvik.system.NativeStart.run(Native Method)
----- end 22050 -----
我已经开启了严格模式,我确实得到了一些警告磁盘写入的痕迹,但没有一个特别长。这些是严格的模式跟踪:
> > 04-30 10:29:50.053: D/StrictMode(22229): StrictMode policy violation;
> > ~duration=35 ms: android.os.StrictMode$StrictModeDiskReadViolation:
> > policy=31 violation=2 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1089)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > libcore.io.BlockGuardOs.open(BlockGuardOs.java:106) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > libcore.io.IoBridge.open(IoBridge.java:390) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > java.io.FileOutputStream.(FileOutputStream.java:88) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.io.FileOutputStream.(FileOutputStream.java:73) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttDefaultFilePersistence.put(MqttDefaultFilePersistence.java:162)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientState.send(ClientState.java:397)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.internalSend(ClientComms.java:81)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:118)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttTopic.publish(MqttTopic.java:62)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MqttWorker.publish(MqttWorker.java:177) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.sendResponse(ApplicationController.java:238)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.onLoadComplete(ApplicationController.java:358)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MainActivity$CustomWebViewClient.onPageFinished(MainActivity.java:95)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:275)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.Handler.dispatchMessage(Handler.java:99) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > android.os.Looper.loop(Looper.java:137) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > android.app.ActivityThread.main(ActivityThread.java:4424) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invokeNative(Native Method) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invoke(Method.java:511) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > dalvik.system.NativeStart.main(Native Method) 04-30 10:29:50.053:
>
>
> > D/StrictMode(22229): StrictMode policy violation; ~duration=26 ms:
> > android.os.StrictMode$StrictModeDiskWriteViolation: policy=31
> > violation=1 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1063)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > libcore.io.BlockGuardOs.write(BlockGuardOs.java:190) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > libcore.io.IoBridge.write(IoBridge.java:447) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > java.io.FileOutputStream.write(FileOutputStream.java:187) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttDefaultFilePersistence.put(MqttDefaultFilePersistence.java:163)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientState.send(ClientState.java:397)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.internalSend(ClientComms.java:81)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:118)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttTopic.publish(MqttTopic.java:62)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MqttWorker.publish(MqttWorker.java:177) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.sendResponse(ApplicationController.java:238)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.onLoadComplete(ApplicationController.java:358)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MainActivity$CustomWebViewClient.onPageFinished(MainActivity.java:95)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:275)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.Handler.dispatchMessage(Handler.java:99) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > android.os.Looper.loop(Looper.java:137) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > android.app.ActivityThread.main(ActivityThread.java:4424) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invokeNative(Native Method) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invoke(Method.java:511) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > dalvik.system.NativeStart.main(Native Method) 04-30 10:29:50.053:
>
>
> > D/StrictMode(22229): StrictMode policy violation; ~duration=18 ms:
> > android.os.StrictMode$StrictModeDiskWriteViolation: policy=31
> > violation=1 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1063)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > libcore.io.BlockGuardOs.write(BlockGuardOs.java:190) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > libcore.io.IoBridge.write(IoBridge.java:447) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > java.io.FileOutputStream.write(FileOutputStream.java:187) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttDefaultFilePersistence.put(MqttDefaultFilePersistence.java:165)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientState.send(ClientState.java:397)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.internalSend(ClientComms.java:81)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:118)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttTopic.publish(MqttTopic.java:62)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MqttWorker.publish(MqttWorker.java:177) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.sendResponse(ApplicationController.java:238)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.onLoadComplete(ApplicationController.java:358)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MainActivity$CustomWebViewClient.onPageFinished(MainActivity.java:95)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:275)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.Handler.dispatchMessage(Handler.java:99) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > android.os.Looper.loop(Looper.java:137) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > android.app.ActivityThread.main(ActivityThread.java:4424) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invokeNative(Native Method) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invoke(Method.java:511) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > dalvik.system.NativeStart.main(Native Method) 04-30 10:29:50.053:
>
>
> > D/StrictMode(22229): StrictMode policy violation; ~duration=2 ms:
> > android.os.StrictMode$StrictModeDiskWriteViolation: policy=31
> > violation=1 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1063)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > libcore.io.BlockGuardOs.fsync(BlockGuardOs.java:96) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.io.FileDescriptor.sync(FileDescriptor.java:71) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttDefaultFilePersistence.put(MqttDefaultFilePersistence.java:167)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientState.send(ClientState.java:397)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.internalSend(ClientComms.java:81)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:118)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttTopic.publish(MqttTopic.java:62)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MqttWorker.publish(MqttWorker.java:177) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.sendResponse(ApplicationController.java:238)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.onLoadComplete(ApplicationController.java:358)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MainActivity$CustomWebViewClient.onPageFinished(MainActivity.java:95)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:275)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.Handler.dispatchMessage(Handler.java:99) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > android.os.Looper.loop(Looper.java:137) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > android.app.ActivityThread.main(ActivityThread.java:4424) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invokeNative(Native Method) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invoke(Method.java:511) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > dalvik.system.NativeStart.main(Native Method)
编辑:这是我启用StrictMode的方式
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.build());
编辑2:为了将来参考并添加问题的原因,我想我应该在这里放一些东西供搜索引擎查找。
事实证明问题出在我们拥有的基于Node.JS的经纪人mqttjs上。它可能只是我们的实现,我没有测试mqttjs开发人员的例子。在发布了一定数量的消息后,经纪人停止发布并返回令牌。 MQTT客户端在另一个线程中运行,但由于它是从UI线程调用的,因此导致了一些问题。
无论如何,我们改为Mosquitto,现在一切都运转良好。
答案 0 :(得分:1)
您正尝试在UI线程上进行长时间运行的网络通信。这会阻止UI更新,这很糟糕。 Android检测到这一事实并将其作为ANR处理。
我不知道你的代码是什么位,但在这里的某处:
se.chalmers.pd.dashboard.ApplicationController.sendResponse(ApplicationController.java:238)
你应该把MQTT放在一个新线程中。如果您因为MQTT工作完成而需要更新UI,请使用Handler。
答案 1 :(得分:1)
由于您已启用严格模式,因此在UI线程中不允许您访问磁盘或网络。
读取堆栈跟踪,很明显,对MqttWorker.publish的调用会导致磁盘访问。您应该将此调用放在AsyncTask或除UI之外的其他线程中。
答案 2 :(得分:1)
你的ANR发生在
org.eclipse.paho.client.mqttv3.internal.MqttDeliveryTokenImpl.waitUntilSent
因为此方法调用Object.wait
,可能正在等待另一个线程通知它。
你的电话显然在这里
se.chalmers.pd.dashboard.MqttWorker.publish(MqttWorker.java:177)
在您的WebClient实现中调用
se.chalmers.pd.dashboard.MainActivity$CustomWebViewClient.onPageFinished(MainActivity.java:89)
在UI线程上调用此onPageFinished
方法。呼叫MqttTopic.publish
是一个网络呼叫,显然会阻塞,直到呼叫完成。您应该将它放在Thread或AsyncTask中,这样它就不会锁定UI Thread。