0

假设我有以下课程......

 private class SignalRConnection extends Thread {

        private final int CONNECT = 0;
        private final int STOP = 1;

        private SignalRHandler signalRHandler = null;

        @Override
        public void run() {
            Looper.prepare();
            signalRHandler = new SignalRHandler();
            Looper.loop();
            signalRHandler.sendMessage(Message.obtain(null, CONNECT));
        }

        public void end() {
            Message msg = Message.obtain(null, STOP);
            Bundle bundle = new Bundle();
            bundle.putBoolean(IMMORTAL, false);
            msg.setData(bundle);
            signalRHandler.sendMessage(msg);
        }

        public void immortal() {
            Message msg = Message.obtain(null, STOP);
            Bundle bundle = new Bundle();
            bundle.putBoolean(IMMORTAL, true);
            msg.setData(bundle);
            signalRHandler.sendMessage(msg);
        }

        private class SignalRHandler extends Handler {

            private final SignalRClient client;

            SignalRHandler() {
                client = new SignalRClient(getResources().getString(R.string.notification_host), "issueshub");
            }

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case CONNECT:
                        client.connect();
                        break;
                    case STOP:
                        /*
                         * Clean up, and terminate.
                         */
                        Bundle b = msg.getData();
                        Boolean isImmortal = b.getBoolean(SignalRService.IMMORTAL);

                        client.disconnect();
                        if (isImmortal) {
                            client.immortal();
                        }
                        break;
                }
            }
        } // End of SignalRHandler
    } // End of SignalR Connection

并从...开始

    signalRConn = new SignalRConnection();
    signalRConn.start();

问题是SignalRHandler无法接收从主类发送的消息。

我确定我做错了什么请指教...

4

0 回答 0