I have a thread in java / Android like this:
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
update_i();
}
};
@Override
protected void onStart() {
super.onStart();
Thread myThread = new Thread(new Runnable() {
public void run() {
while (true) {
try {
handler.sendMessage(handler.obtainMessage());
Thread.sleep(timer);
} catch (Throwable t) {
}
}
}
});
myThread.start();
}
The thread works fine when running my application. But I want to start / restart the stream using the button.
Button.OnClickListener StartButtonOnClickListener = new Button.OnClickListener() {
@Override
public void onClick(View v) {
}
};
If I copy a thread to a button, I simply create a new thread every time the user clicks on the button. I want to start a thread when the user first clicks on the kill button and starts from the beginning if the user clicks the button a second time (I don’t want to start the second thread).
source
share