how to change the status of togglebutton in pubnub subscribe callback

时间:2016-04-15 14:53:27

标签: android pubnub

i would like to change the state of toggle button when pubnub subscribe receive a success callback if the message is equal to a preset string the togglebutton.setchecked(true) else it will call a toast and says connection problem how can this be done in an async process UPDATE :

After Trying herry suggestion :

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();
}

i received the following log cat when i send the message (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)

2 个答案:

答案 0 :(得分:1)

我没有使用Pubnub但你可以在你的代码中这样做: 注意:我假设此回调在主线程上

在你的活动类:

private ToggleButton  mBtnToggle;

/* Subscribe to the demo_tutorial channel */
try {
    pubnub.subscribe("demo_tutorial", 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.str.equal(strData)){
                //Set True
                mBtnToggle.setChecked(true)
            }
        }

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

答案 1 :(得分:1)

这是工作代码:

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();
}