Ich möchte den Zustand der Toggle-Taste ändern, wenn pubnub subscribe einen Erfolg Rückruf erhalten, wenn die Nachricht entspricht einer voreingestellten Zeichenfolge die togglebutton.setchecked (true) sonst ist es einen Toast rufen und sagt Verbindungsproblem , wie diese UPDATE in einem Asynchron-Prozess durchgeführt werden kann:Wie ändere ich den Status von togglebutton in pubnub subscribe Callback
Nach dem Versuch herry Vorschlag:
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 die folgenden Protokoll Katze erhalten, wenn ich die Nachricht (RL1 senden : 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)
Teilen Sie einige Code-Schnipsel von pubnub Subscribe Callback – Herry