0

android.os.MessageQueue.hasMessages(MessageQueue.java:~361)当我从 ServiceConnection() 对象中的 onServiceConnected 函数向服务发送消息时,为什么会收到错误消息?我期望的结果是看到打印有字符串“MyActivity.log:responseString:response”的日志,但有些东西挂在某处。为了得到错误的转储,我通过点击屏幕并等待 5 秒钟导致了 ANR。这是在日志中看到的异常,但真正的问题是在我的代码中的某个地方,我导致某些东西挂起。如果需要任何澄清,请告诉我。

我的活动和服务很一般。相关部分贴在下面。

我的活动.java

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName name, IBinder service) {
        sendToServiceMessenger = new Messenger(service);
        registerClient();
        sendMessageToService();
    }

    public void onServiceDisconnected(ComponentName name) {
        deregisterClient();
    }
};

private void registerClient() {
    Message msg = Message.obtain(null, MessageType.MSG_REGISTER_CLIENT);
    msg.replyTo = mMessenger;
    try {
        sendToServiceMessenger.send(msg);
    } catch (RemoteException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

// Handles messages coming from the Service
private final Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
        switch(msg.what) {
            case MessageType.MSG_RESPONSE_FROM_SERVICE:
                String responseString = (String) msg.obj;
                Log.v(TAG, "responseString: " + responseString);
            break;
        }
    }
};
private final Messenger mMessenger = new Messenger(mHandler);

private void sendMessageToService() {
    if (null == sendToServiceMessenger) {
        return;
    }

    Message msg = Message.obtain(null, MessageType.MSG_SEND_TO_SERVICE);
    msg.replyTo = mMessenger;
    try {
        sendToServiceMessenger.send(msg);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

我的服务.java

public Handler receiveHandler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case MessageType.MSG_REGISTER_CLIENT:
                mClients.add(msg.replyTo);
                break;
            case MessageType.MSG_SEND_TO_SERVICE:
                String responseString = "response";
                sendString(MessageType.MSG_RESPONSE_FROM_SERVICE, responseString);
                break;
        }
    }
}

private void sendString (int messageType, String stringToSend) {
    Message msg = Message.obtain(null, messageType);
    msg.obj = stringToSend;
    for (int i = mClients.size() - 1; i >= 0; i--) {
        try {
            mClients.get(i).send(msg);
        } catch (RemoteException e) {
            mClients.remove(i);
        }
    }
}

由于我收到的错误消息是,调试不会去任何地方:

日志

01-04 03:09:44.555   911   992 E ActivityManager: ANR in com.mypackage.mysubpackage (com.mypackage.mysubpackage/MyActivity)
01-04 03:09:44.555   911   992 E ActivityManager: Reason: keyDispatchingTimedOut
01-04 03:09:44.555   911   992 E ActivityManager: Load: 7.63 / 7.41 / 7.27
01-04 03:09:44.555   911   992 E ActivityManager: CPU usage from 5183ms to 0ms ago with 99% awake:
01-04 03:09:44.555   911   992 E ActivityManager:   98% 7458/com.mypackage.mysubpackage: 98% user + 0% kernel
01-04 03:09:44.555   911   992 E ActivityManager:   1.5% 911/system_server: 1.3% user + 0.1% kernel / faults: 150 minor
01-04 03:09:44.555   911   992 E ActivityManager:   0.7% 364/zygote: 0.5% user + 0.1% kernel / faults: 292 minor
01-04 03:09:44.555   911   992 E ActivityManager:   0.7% 412/mm-pp-daemon: 0% user + 0.7% kernel
01-04 03:09:44.555   911   992 E ActivityManager:   0.7% 2127/adbd: 0% user + 0.7% kernel
01-04 03:09:44.555   911   992 E ActivityManager:   0.3% 3/ksoftirqd/0: 0% user + 0.3% kernel
01-04 03:09:44.555   911   992 E ActivityManager:   0.3% 366/mediaserver: 0.1% user + 0.1% kernel
01-04 03:09:44.555   911   992 E ActivityManager:   0.3% 1492/mpdecision: 0% user + 0.3% kernel
01-04 03:09:44.555   911   992 E ActivityManager:   0.3% 2499/logcat: 0.1% user + 0.1% kernel
01-04 03:09:44.555   911   992 E ActivityManager:   0.3% 6722/kworker/0:2: 0% user + 0.3% kernel
01-04 03:09:44.555   911   992 E ActivityManager:   0% 126/irq/342-atmel_m: 0% user + 0% kernel
01-04 03:09:44.555   911   992 E ActivityManager:   0.1% 363/surfaceflinger: 0% user + 0.1% kernel
01-04 03:09:44.555   911   992 E ActivityManager:   0% 484/lowi-server: 0% user + 0% kernel
01-04 03:09:44.555   911   992 E ActivityManager:   0.1% 517/sensors.qcom: 0% user + 0.1% kernel / faults: 1 minor
01-04 03:09:44.555   911   992 E ActivityManager:   0% 6843/kworker/0:0: 0% user + 0% kernel
01-04 03:09:44.555   911   992 E ActivityManager:   0.1% 6904/kworker/u:14: 0% user + 0.1% kernel
01-04 03:09:44.555   911   992 E ActivityManager:   0.1% 7434/logcat: 0% user + 0.1% kernel / faults: 1 minor
01-04 03:09:44.555   911   992 E ActivityManager:   0.1% 7502/kworker/1:1: 0.1% user + 0% kernel
01-04 03:09:44.555   911   992 E ActivityManager:  +0% 7508/com.mypackage.msapu: 0% user + 0% kernel
01-04 03:09:44.555   911   992 E ActivityManager:  +0% 7523/com.mypackage.msapm: 0% user + 0% kernel
01-04 03:09:44.555   911   992 E ActivityManager: 50% TOTAL: 48% user + 1.2% kernel + 0% iowait
01-04 03:09:44.555   911   992 E ActivityManager: CPU usage from 1004ms to 1519ms later:
01-04 03:09:44.555   911   992 E ActivityManager:   100% 7458/com.mypackage.mysubpackage: 100% user + 0% kernel
01-04 03:09:44.555   911   992 E ActivityManager:     100% 7458/mypackage: 100% user + 0% kernel
01-04 03:09:44.555   911   992 E ActivityManager:   3.8% 911/system_server: 0% user + 3.8% kernel
01-04 03:09:44.555   911   992 E ActivityManager:     1.9% 992/InputDispatcher: 0% user + 1.9% kernel
01-04 03:09:44.555   911   992 E ActivityManager: 52% TOTAL: 50% user + 1.9% kernel
01-04 03:09:44.555   911  7560 E ActivityManager: Error reading /data/anr/traces.txt
01-04 03:09:44.555   911  7560 E ActivityManager: java.io.FileNotFoundException: /data/anr/traces.txt: open failed: ENOENT (No such file or directory)
01-04 03:09:44.555   911  7560 E ActivityManager:   at libcore.io.IoBridge.open(IoBridge.java:416)
01-04 03:09:44.555   911  7560 E ActivityManager:   at java.io.FileInputStream.<init>(FileInputStream.java:78)
01-04 03:09:44.555   911  7560 E ActivityManager:   at android.os.FileUtils.readTextFile(FileUtils.java:146)
01-04 03:09:44.555   911  7560 E ActivityManager:   at com.android.server.am.ActivityManagerService$12.run(ActivityManagerService.java:8576)
01-04 03:09:44.555   911  7560 E ActivityManager: Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
01-04 03:09:44.555   911  7560 E ActivityManager:   at libcore.io.Posix.open(Native Method)
01-04 03:09:44.555   911  7560 E ActivityManager:   at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-04 03:09:44.555   911  7560 E ActivityManager:   at libcore.io.IoBridge.open(IoBridge.java:400)
01-04 03:09:44.555   911  7560 E ActivityManager:   ... 3 more
01-04 03:09:44.665   361   361 I DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
01-04 03:09:44.665   361   361 I DEBUG   : Build fingerprint: 'qcom/msm8974/msm8974:4.2.2/JDQ39/eng.lnxbuild.20130909.193415:userdebug/test-keys'
01-04 03:09:44.665   361   361 I DEBUG   : Revision: '0'
01-04 03:09:44.665   361   361 I DEBUG   : pid: 7458, tid: 7458, name: mysubpackage  >>> com.mypackage.mysubpackage <<<
01-04 03:09:44.665   361   361 I DEBUG   : signal 6 (SIGABRT), code -6 (?), fault addr --------
01-04 03:09:44.755   361   361 I DEBUG   :     r0 42846260  r1 42846260  r2 014294a0  r3 00000000
01-04 03:09:44.755   361   361 I DEBUG   :     r4 014294a1  r5 41607d5c  r6 40bfd0d0  r7 00000000
01-04 03:09:44.755   361   361 I DEBUG   :     r8 00000001  r9 00000004  sl 00000000  fp 0000048f
01-04 03:09:44.755   361   361 I DEBUG   :     ip 00000000  sp beea86a8  lr 00000000  pc 5c9433f2  cpsr 80000030
01-04 03:09:44.755   361   361 I DEBUG   :     d0  0000000000000000  d1  0000000000000000
01-04 03:09:44.755   361   361 I DEBUG   :     d2  000003e800000000  d3  0000000000000004
01-04 03:09:44.755   361   361 I DEBUG   :     d4  0079007400690076  d5  0065007200680054
01-04 03:09:44.755   361   361 I DEBUG   :     d6  006d002e00640061  d7  0028006e00690061
01-04 03:09:44.755   361   361 I DEBUG   :     d8  41a0000000000000  d9  442f0000442e8000
01-04 03:09:44.755   361   361 I DEBUG   :     d10 00000000442f0000  d11 0000000000000000
01-04 03:09:44.755   361   361 I DEBUG   :     d12 0000000000000000  d13 0000000000000000
01-04 03:09:44.755   361   361 I DEBUG   :     d14 0000000000000000  d15 0000000000000000
01-04 03:09:44.755   361   361 I DEBUG   :     d16 00000001001e0003  d17 0000000100160101
01-04 03:09:44.755   361   361 I DEBUG   :     d18 0074007300790073  d19 004e002e006d0065
01-04 03:09:44.755   361   361 I DEBUG   :     d20 0076006900740061  d21 0061007400530065
01-04 03:09:44.755   361   361 I DEBUG   :     d22 006d002e00740072  d23 0028006e00690061
01-04 03:09:44.755   361   361 I DEBUG   :     d24 0028002700260024  d25 002a0028002a0029
01-04 03:09:44.755   361   361 I DEBUG   :     d26 0000000000000000  d27 0000000000000000
01-04 03:09:44.755   361   361 I DEBUG   :     d28 001e001d001c001b  d29 0020001f001e001c
01-04 03:09:44.755   361   361 I DEBUG   :     d30 002a002a002a002a  d31 0000000000000000
01-04 03:09:44.755   361   361 I DEBUG   :     scr 6800009e
01-04 03:09:44.755   361   361 I DEBUG   : 
01-04 03:09:44.755   361   361 I DEBUG   : backtrace:
01-04 03:09:44.755   361   361 I DEBUG   :     #00  pc 0001f3f2  /dev/ashmem/dalvik-jit-code-cache (deleted)
01-04 03:09:44.755   361   361 I DEBUG   : 
01-04 03:09:44.755   361   361 I DEBUG   : stack:
01-04 03:09:44.755   361   361 I DEBUG   :          beea8668  00000004  
01-04 03:09:44.755   361   361 I DEBUG   :          beea866c  41607d5c  
01-04 03:09:44.755   361   361 I DEBUG   :          beea8670  0000048f  
01-04 03:09:44.755   361   361 I DEBUG   :          beea8674  40883f14  /system/lib/libdvm.so (dvmCheckBefore+556)
01-04 03:09:44.755   361   361 I DEBUG   :          beea8678  40bfd0d0  
01-04 03:09:44.755   361   361 I DEBUG   :          beea867c  40bfd0d0  
01-04 03:09:44.755   361   361 I DEBUG   :          beea8680  00004000  
01-04 03:09:44.755   361   361 I DEBUG   :          beea8684  00000001  
01-04 03:09:44.755   361   361 I DEBUG   :          beea8688  5897de8a  /data/dalvik-cache/system@framework@framework.jar@classes.dex
01-04 03:09:44.755   361   361 I DEBUG   :          beea868c  5897de88  /data/dalvik-cache/system@framework@framework.jar@classes.dex
01-04 03:09:44.755   361   361 I DEBUG   :          beea8690  41607d5c  
01-04 03:09:44.755   361   361 I DEBUG   :          beea8694  40bfd0d0  
01-04 03:09:44.755   361   361 I DEBUG   :          beea8698  00001207  
01-04 03:09:44.755   361   361 I DEBUG   :          beea869c  40876b80  /system/lib/libdvm.so
01-04 03:09:44.755   361   361 I DEBUG   :          beea86a0  df0027ad  
01-04 03:09:44.755   361   361 I DEBUG   :          beea86a4  00000000  
01-04 03:09:44.755   361   361 I DEBUG   :     #00  beea86a8  00000000  
01-04 03:09:44.755   361   361 I DEBUG   :          beea86ac  40bfd0d0  
01-04 03:09:44.755   361   361 I DEBUG   :          beea86b0  40911c50  /system/lib/libdvm.so
01-04 03:09:44.755   361   361 I DEBUG   :          beea86b4  5767eba0  /dev/ashmem/dalvik-LinearAlloc (deleted)
01-04 03:09:44.755   361   361 I DEBUG   :          beea86b8  fffffe2c  
01-04 03:09:44.755   361   361 I DEBUG   :          beea86bc  beea86dc  [stack]
01-04 03:09:44.755   361   361 I DEBUG   :          beea86c0  beea8780  [stack]
01-04 03:09:44.755   361   361 I DEBUG   :          beea86c4  00000000  
01-04 03:09:44.755   361   361 I DEBUG   :          beea86c8  4232367c  /dev/ashmem/dalvik-heap (deleted)
01-04 03:09:44.755   361   361 I DEBUG   :          beea86cc  40884164  /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+188)
01-04 03:09:44.755   361   361 I DEBUG   :          beea86d0  beea8700  [stack]
01-04 03:09:44.765   361   361 I DEBUG   :          beea86d4  4089df2b  /system/lib/libdvm.so (dvmHashTableLookup(HashTable*, unsigned int, void*, int (*)(void const*, void const*), bool)+50)
01-04 03:09:44.765   361   361 I DEBUG   :          beea86d8  00000000  
01-04 03:09:44.765   361   361 I DEBUG   :          beea86dc  58b17fb8  /data/dalvik-cache/system@framework@framework.jar@classes.dex
01-04 03:09:44.765   361   361 I DEBUG   :          beea86e0  41607ec4  
01-04 03:09:44.765   361   361 I DEBUG   :          beea86e4  575ea390  /dev/ashmem/dalvik-LinearAlloc (deleted)
01-04 03:09:44.765   361   361 I DEBUG   : 
01-04 03:09:44.765   361   361 I DEBUG   : memory near r0:
...
01-04 03:09:44.785   361   361 I DEBUG   : memory near r6:
...
01-04 03:09:44.785   361   361 I DEBUG   : memory near sp:
...
01-04 03:09:44.785   361   361 I DEBUG   : code around pc:
...
01-04 03:09:44.795   361   361 I DEBUG   : code around lr:
01-04 03:09:44.795   361   361 I DEBUG   :     00000000 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     00000010 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     00000020 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     00000030 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     00000040 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     00000050 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     00000060 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     00000070 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     00000080 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     00000090 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     000000a0 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     000000b0 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     000000c0 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     000000d0 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     000000e0 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   :     000000f0 ffffffff ffffffff ffffffff ffffffff  
01-04 03:09:44.795   361   361 I DEBUG   : 
01-04 03:09:44.795   361   361 I DEBUG   : [Dalvik] Can't dump ETB. /dev/coresight-etb doesn't exist 
01-04 03:09:44.795   361   361 I DEBUG   : 
01-04 03:09:44.795   361   361 I DEBUG   : [Dalvik] Crash in thread 1 at trace address 5c9433ac trace size 164
01-04 03:09:44.795   361   361 I DEBUG   : [Dalvik] Trace content dump:
01-04 03:09:44.795   361   361 I DEBUG   : dump memory region: 5c9433ac --> 5c943450
01-04 03:09:44.795   361   361 I DEBUG   : 5c9433ac  f8d56869 696b8010 6a2f69ec 0001ea4f 
01-04 03:09:44.795   361   361 I DEBUG   : 5c9433bc  0100ea4f 69c9b1f1 60692900 801ff000 
01-04 03:09:44.795   361   361 I DEBUG   : 5c9433cc  f04fb311 6a4b38ff 42bb6a0a 800df2c0 
01-04 03:09:44.795   361   361 I DEBUG   : 5c9433dc  8009f300 0804ebb2 8007f000 f04fbf8c 
01-04 03:09:44.795   361   361 I DEBUG   : 5c9433ec  f04f38ff f1c80801 f1b80800 f77f0f00 
01-04 03:09:44.795   361   361 I DEBUG   : 5c9433fc  e00eafdd f8c560a8 616b8010 60a8e00f 
01-04 03:09:44.795   361   361 I DEBUG   : 5c94340c  8010f8c5 e011616b f8c560a8 616b8010 
01-04 03:09:44.795   361   361 I DEBUG   : 5c94341c  60a8e008 8010f8c5 e007616b f8dfde00 
01-04 03:09:44.795   361   361 I DEBUG   : 5c94342c  e001004c 0040f8df 47886ef1 4300e000 
01-04 03:09:44.795   361   361 I DEBUG   : 5c94343c  47806e70 5897de90 4300e000 47806e70 
01-04 03:09:44.795   361   361 I DEBUG   : 5c94344c  5897de4c 
01-04 03:09:44.795   361   361 I DEBUG   : 
01-04 03:09:44.795   361   361 I DEBUG   : [Dalvik] Crash in Landroid/os/MessageQueue;enqueueSyncBarrier(IJ)
01-04 03:09:44.795   361   361 I DEBUG   : [Dalvik] First 4 trace runs (if any):
01-04 03:09:44.795   361   361 I DEBUG   :   Trace 0 start offset: 0x1e len: 3
01-04 03:09:44.795   361   361 I DEBUG   :   Trace 1 start offset: 0x16 len: 1
01-04 03:09:44.795   361   361 I DEBUG   : 
01-04 03:09:44.795   361   361 I DEBUG   : [Dalvik] Dumping method DEX
01-04 03:09:44.795   361   361 I DEBUG   : dump memory region: 5897de4c --> 5897deb0
01-04 03:09:44.795   361   361 I DEBUG   : 5897de4c  63f2061d 04d80014 64f50103 00710014 
01-04 03:09:44.805   361   361 I DEBUG   : 5897de5c  0000721e 03f5000c 0212002c 001061f4 
01-04 03:09:44.805   361   361 I DEBUG   : 5897de6c  00000416 04070431 000e0438 000c0138 
01-04 03:09:44.805   361   361 I DEBUG   : 5897de7c  002014f3 07040431 0006043c 11f41207 
01-04 03:09:44.805   361   361 I DEBUG   : 5897de8c  f528001c 00080238 001c01f7 001c20f7 
01-04 03:09:44.805   361   361 I DEBUG   : 5897de9c  030f061e 001c01f7 001060f7 040dfa28 
01-04 03:09:44.805   361   361 I DEBUG   : 5897deac  0427061e 
01-04 03:09:44.805   361   361 I DEBUG   : 
01-04 03:09:44.805   361   361 I DEBUG   : [Dalvik] Stack walk: 
01-04 03:09:44.805   361   361 I DEBUG   : [ 0] 0x41607d5c Landroid/os/MessageQueue;enqueueSyncBarrier(IJ)
01-04 03:09:44.805   361   361 I DEBUG   : [ 1] 0x41607d94 Landroid/os/Looper;postSyncBarrier(I)
01-04 03:09:44.805   361   361 I DEBUG   : [ 2] 0x41607db8 Landroid/view/ViewRootImpl;scheduleTraversals(V)
01-04 03:09:44.805   361   361 I DEBUG   : [ 3] 0x41607de0 Landroid/view/ViewRootImpl;handleAppVisibility(VZ)
01-04 03:09:44.805   361   361 I DEBUG   : [ 4] 0x41607e00 Landroid/view/ViewRootImpl$ViewRootHandler;handleMessage(VL)
01-04 03:09:44.805   361   361 I DEBUG   : [ 5] 0x41607e64 Landroid/os/Handler;dispatchMessage(VL)
01-04 03:09:44.805   361   361 I DEBUG   : [ 6] 0x41607e84 Landroid/os/Looper;loop(V)
01-04 03:09:44.805   361   361 I DEBUG   : [ 7] 0x41607ec4 Landroid/app/ActivityThread;main(VL)
01-04 03:09:44.805   361   361 I DEBUG   : [Dalvik] Dumping dalvik stack errno: Success
01-04 03:09:44.805   361   361 I DEBUG   : 
01-04 03:09:44.805   361   361 I DEBUG   : [Dalvik] Dumping 128340 bytes from codeCache at 0x5c924000 into /data/user/log_7458.bin
01-04 03:09:45.075   361   361 I DEBUG   : [Dalvik] Dumping codeCache symbols into /data/user/log_7458.sym
01-04 03:09:45.225   361   361 I DEBUG   : [Dalvik] Dumping codecache to file errno: Success
01-04 03:09:45.225   361   361 I DEBUG   : [Dalvik] Dumping codecache errno: Success
01-04 03:09:45.325  7458  7463 W dalvikvm: threadid=3: spin on suspend #2 threadid=1 (pcf=0)
01-04 03:09:45.325  7458  7463 I dalvikvm: "Signal Catcher" daemon prio=5 tid=3 RUNNABLE
01-04 03:09:45.325  7458  7463 I dalvikvm:   | group="system" sCount=0 dsCount=0 obj=0x42321e78 self=0x5ae00590
01-04 03:09:45.325  7458  7463 I dalvikvm:   | sysTid=7463 nice=0 sched=0/0 cgrp=apps handle=1096951768
01-04 03:09:45.325  7458  7463 I dalvikvm:   | state=R schedstat=( 1487602 5591875 42 ) utm=0 stm=0 core=1
01-04 03:09:45.325  7458  7463 I dalvikvm:   at dalvik.system.NativeStart.run(Native Method)
01-04 03:09:45.325  7458  7463 I dalvikvm: 
01-04 03:09:45.325  7458  7463 I dalvikvm: "main" prio=5 tid=1 RUNNABLE JIT
01-04 03:09:45.325  7458  7463 I dalvikvm:   | group="main" sCount=1 dsCount=0 obj=0x42097b38 self=0x40bfd0d0
01-04 03:09:45.325  7458  7463 I dalvikvm:   | sysTid=7458 nice=0 sched=0/0 cgrp=apps handle=1074855132
01-04 03:09:45.325  7458  7463 I dalvikvm:   | state=R schedstat=( 9065612838 633459565 4287 ) utm=889 stm=17 core=1
01-04 03:09:45.325  7458  7463 I dalvikvm:   at android.os.MessageQueue.enqueueSyncBarrier(MessageQueue.java:~239)
01-04 03:09:45.325  7458  7463 I dalvikvm:   at android.os.Looper.postSyncBarrier(Looper.java:227)
01-04 03:09:45.325  7458  7463 I dalvikvm:   at android.view.ViewRootImpl.scheduleTraversals(ViewRootImpl.java:973)
01-04 03:09:45.325  7458  7463 I dalvikvm:   at android.view.ViewRootImpl.handleAppVisibility(ViewRootImpl.java:802)
01-04 03:09:45.325  7458  7463 I dalvikvm:   at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2867)
01-04 03:09:45.325  7458  7463 I dalvikvm:   at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 03:09:45.325  7458  7463 I dalvikvm:   at android.os.Looper.loop(Looper.java:137)
01-04 03:09:45.325  7458  7463 I dalvikvm:   at android.app.ActivityThread.main(ActivityThread.java:5074)
01-04 03:09:45.325  7458  7463 I dalvikvm:   at java.lang.reflect.Method.invokeNative(Native Method)
01-04 03:09:45.325  7458  7463 I dalvikvm:   at java.lang.reflect.Method.invoke(Method.java:511)
01-04 03:09:45.325   911   966 I BootReceiver: Copying /data/tombstones/tombstone_06 to DropBox (SYSTEM_TOMBSTONE)
01-04 03:09:45.335  7458  7463 I dalvikvm:   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-04 03:09:45.335  7458  7463 I dalvikvm:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-04 03:09:45.335  7458  7463 I dalvikvm:   at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

此代码有效:

public class ServiceTestActivity extends Activity {

    public static final int MSG_REGISTER_CLIENT         = 0x00000001;
    public static final int MSG_SEND_TO_SERVICE         = 0x00000002;
    public static final int MSG_RESPONSE_FROM_SERVICE   = 0x00000003;

    private static final String TAG = ServiceTestActivity.class.getName();

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder service) {
            sendToServiceMessenger = new Messenger(service);
            registerClient();
            sendMessageToService();
        }

        public void onServiceDisconnected(ComponentName name) {
            //deregisterClient();
        }
    };

    // Handles messages coming from the Service
    private final Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case MSG_RESPONSE_FROM_SERVICE:
                    String responseString = (String) msg.obj;
                    Log.v(TAG, "responseString: " + responseString);
                break;
            }
        }
    };

    private Messenger sendToServiceMessenger;
    private final Messenger mMessenger = new Messenger(mHandler);

    private void registerClient() {
        Message msg = Message.obtain(null, MSG_REGISTER_CLIENT);
        msg.replyTo = mMessenger;
        try {
            sendToServiceMessenger.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void sendMessageToService() {
        if (null == sendToServiceMessenger) {
            return;
        }

        Message msg = Message.obtain(null, MSG_SEND_TO_SERVICE);
        msg.replyTo = mMessenger;
        try {
            sendToServiceMessenger.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bindService(new Intent(this, ServiceTest.class).putExtra("messenger", mMessenger), mConnection, Context.BIND_AUTO_CREATE);

    }
}

和服务:

public class ServiceTest extends Service {

    /* INSTANCE FIELDS */
        final List<Messenger> mClients = new LinkedList<Messenger>();

    @Override
    public IBinder onBind(Intent intent) {
        return new Messenger(receiveHandler).getBinder();
    }

    public Handler receiveHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case ServiceTestActivity.MSG_REGISTER_CLIENT:
                    mClients.add(msg.replyTo);
                    break;
                case ServiceTestActivity.MSG_SEND_TO_SERVICE:
                    String responseString = "response";
                    sendString(ServiceTestActivity.MSG_RESPONSE_FROM_SERVICE, responseString);
                    break;
            }
        }
    };

    private void sendString (int messageType, String stringToSend) {
        Message msg = Message.obtain(null, messageType);
        msg.obj = stringToSend;
        for (int i = mClients.size() - 1; i >= 0; i--) {
            try {
                mClients.get(i).send(msg);
            } catch (RemoteException e) {
                mClients.remove(i);
            }
        }
    }
}
于 2013-09-14T07:42:40.847 回答
0

如果您花费太多时间来响应关键调度事件,则会发生此错误。
Timer 设置在一个键事件上,该事件会导致您在超时时看到异常(即,如果键调度需要太长时间才能返回)

您应该考虑在主线程上做更少的工作。

用于new Thread(myRunnable).start()在主线程之外执行非 UiThread 代码

于 2013-09-13T06:57:44.633 回答