如果消息等于预设字符串,我想更改pubnub订阅收到成功回调时切换按钮的状态。togglebutton.setchecked(true)
否则会叫吐司说连接有问题
如何在异步过程中完成
更新:

在尝试Herry建议之后:

try {
    pubnub.subscribe("reply", new Callback() {
        public void successCallback(String channel, Object message) {
            System.out.println(message);

            //Match your String Here and set Toggle Btn status here
            if(message.toString().equals("RL1:1")){
                //Set True
                toggleButton1.setChecked(true);
            }
            toggleButton1.setBackgroundResource(R.drawable.light100);
        }

        public void errorCallback(String channel, PubnubError error) {
            System.out.println(error.getErrorString());
        }
    });
}
catch (PubnubException e) {
    e.printStackTrace();
}


发送消息(RL1:1)时,收到以下日志猫:

04-15 19:42:10.594 17016-17188/wadihmaaloufengineering.smarthome E/AndroidRuntime: FATAL EXCEPTION: Subscribe-Manager-115480684-6
Process: wadihmaaloufengineering.smarthome, PID: 17016
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7599)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1139)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5033)
at android.view.View.invalidateInternal(View.java:12973)
at android.view.View.invalidate(View.java:12937)
at android.view.View.invalidate(View.java:12921)
at android.widget.TextView.checkForRelayout(TextView.java:8142)
at android.widget.TextView.setText(TextView.java:4862)
at android.widget.TextView.setText(TextView.java:4686)
at android.widget.TextView.setText(TextView.java:4661)
at android.widget.ToggleButton.syncTextState(ToggleButton.java:81)
at android.widget.ToggleButton.setChecked(ToggleButton.java:75)
at wadihmaaloufengineering.smarthome.lighting$1.successCallback(lighting.java:88)
at com.pubnub.api.Callback.successWrapperCallbackV2(Unknown Source)
at com.pubnub.api.PubnubCoreAsync.invokeSubscribeCallbackV2(Unknown Source)
at com.pubnub.api.PubnubCoreAsync.access$600(Unknown Source)
at com.pubnub.api.PubnubCoreAsync$2.v2Handler(Unknown Source)
at com.pubnub.api.PubnubCoreAsync$2.handleResponse(Unknown Source)
at com.pubnub.api.SubscribeWorker.process(Unknown Source)
at com.pubnub.api.Worker.run(Unknown Source)
at java.lang.Thread.run(Thread.java:818)

最佳答案

这是工作代码:

try {
    pubnub.subscribe("reply", new Callback() {
        public void successCallback(String channel, Object message) {
            System.out.println(message);
            msg = message.toString();
            //Match your String Here and set Toggle Btn status here
            lighting.this.runOnUiThread(new Runnable(){
                @Override
                public void run(){
                    if(msg.equals("RL1:1")){
                        //Set True
                        toggleButton1.setChecked(true);
                        toggleButton1.setBackgroundResource(R.drawable.light100);
                    }
                }
            });
        };

        public void errorCallback(String channel, PubnubError error) {
            System.out.println(error.getErrorString());
        }

    });
}
catch (PubnubException e) {
    e.printStackTrace();
}

关于android - 如何更改pubnub订阅回调中togglebutton的状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36650458/

10-11 15:08