I have a class ConnectionThread that extends Thread. I am passing the context to it's constructor and i want to invoke a method that enables and disables all the buttons on the UI. This method is in the same class - ConnectionThread. Also i want to popup a Toast in a try catch block in case the connection can't be established.
public ConnectionThread(Context context, BluetoothSocket bluetoothSocket,
Button connectButton, Button disconnectButton,
Button throttleUpButton, Button throttleDownButton,
Button moveForwardButton, Button moveBackwardButton,
Button moveLeftButton, Button moveRightButton, Button exitButton) {
My question is how can i use runOnUiThread() or onPostExecute() to do that?
onPostExecute()(only in Asynctask) runs already on UI thread so no need to create a runOnUiThread inside of it.. In other case if you are inside a thread or background task and want to update some views now you need to call the runOnUiThread because you are now leaving the main thread..
pass the activity instance to the contructor to call the main thread.
example:
activity.runOnUiThread(new Runnable() {
public void run() {
//code here
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With