How to stop or exit "Runnable"
android, java, multithreading
Solution
Edit:
Now that you've added code that involves the Handler it's now clearer where your trouble lies. Some points to understand:
- Android has a general concept that there is the main thread and then there are many other threads.
- Only the main thread may modify the UI
- In later versions of android the main thread is not allowed to do networking...
So how to change the UI based on reading something from the network?
You need to pass a message from another (networking) thread to the main thread. The Handler let's your other thread give a message to the main thread... That is your networking thread can give a task (`Runnable`) to the main thread to perform. The handler posts the task from the networking thread to the main thread.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
intent = getIntent();
setContentView(R.layout.activity_display_message);
message = intent.getStringArrayExtra(MainActivity.EXTRA_MESSAGE);
newtext = (TextView)findViewById(R.id.TextView1);
userName = message[0];
serverIP = message[1];
sendConnectionRequest ();
mHandler = new Handler(); // Handler to update UI
// This is being created in the main thread
// so everything posted will be posted to
// the main thread
mUpdate.start(); // start thread to do something on the network
//before updating the UI
}
public Thread mUpdate = new Thread() {
public void run() {
try {
while (!thread.interrupted()) {
final String line = in.readLine();
Log.i("RESPONSE FROM SERVER", "S: Received Message: '" +line+ "'");
// Define the UI change you want to make
Runnable uiUpdate = new Runnable() {
public void run() {
// modify your UI here.
newtext.setText(line);
}
};
// post the UI change back to the main thread.
mHandler.post(uiUpdate);
}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Error" , "Something Happen");
}
}
public void interrupt() {
try {
super.interrupt();
in.close();
}
catch (IOException e) {}
}
};
May I suggest you also:
protected void onDestroy() {
mUpdate.interrupt();
}
Original Answer (Now defunct)
From my understanding, buttons are on UI thread and Runnable is a separate thread and that's why it doesn't respond to button press immediately coz its busy on separate thread.
That's not correct at all. A Thread is a separate thread not a Runnable. And you need to call `start()` not `run()` on the thread to make the run method execute in its own thread. The whole point of using threads is that one thread will not (usually) be blocked by another being busy.
// <snip>removed for brevity of new answer</snip>
You can also look into an AsyncTask from the android library. This will also run in its own thread if you use it correctly.
Problem
I'm new to android.I'm building app to listen incoming message over WiFi , it works fine for listening and updating UI but when i try to exit it doesn't respond to back button press until it receive message in buffer. From my understanding, buttons are on UI thread and Runnable is a separate thread and that's why it doesn't respond to button press immediately coz its busy on separate thread. So how can i interrupt "Runnable" from back button press? Any help would be much appreciated. ``` public Runnable mUpdate = new Runnable() { public void run() { try { line = in.readLine(); newtext.setText(line); mHandler.post(this); Log.i("RESPONSE FROM SERVER", "S: Received Message: '" +line+ "'"); //onBackPressed(); //threadRunning = true; } catch (IOException e) { // TODO Auto-generated catch block Log.e("Error" , "Something Happen"); } } }; ``` Edit : Sorry, I should have post this earlier , so in "onCreate" , i use handler to call "mUpdate" . Is it the right way to call or not? ``` protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); intent = getIntent(); setContentView(R.layout.activity_display_message); message = intent.getStringArrayExtra(MainActivity.EXTRA_MESSAGE); newtext = (TextView)findViewById(R.id.TextView1); userName = message[0]; serverIP = message[1]; sendConnectionRequest (); mHandler = new Handler(); // Handler to update UI mHandler.post(mUpdate); // post is a method to update UI } ```