如何在按钮 OnClick 中暂停线程
how to pause thread in buttonOnClick
我有一个从 30 倒计时到 1 的线程和一个 button.I 已经启动线程但我不知道如何在 buttonOnClick.This 内暂停该线程是我的代码 reference.Thank 你的帮助。
(如何在 buttonOnClick 中暂停线程)
int n;
TextView box2;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
String string = box2.getText().toString();
int num = Integer.parseInt(string);
num = num - 1;
string = Integer.toString(num);
box2.setText(string);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
class Allthread extends Thread { //backgroud task
@Override
public void run() {
// TODO Auto-generated method stub
for (i = 30; i > 0; i--) {
try {
Thread.sleep(1000);
//handler.sendMessage(handler.obtainMessage());
handler.sendMessage(handler.obtainMessage());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void buttonOnClick(View view) {
//do something when enter is clicked
Button enter = (Button) findViewById(R.id.button);
TextView box1 = (TextView) findViewById(R.id.box1);
n++;
box1.setText("number" + n);
if(n==1){
Allthread T1 = new Allthread();
T1.start();//start the thread
}
// I want pause thread here.
}
}
看到这个link
How to pause/resume thread in Android?
我有一个从 30 倒计时到 1 的线程和一个 button.I 已经启动线程但我不知道如何在 buttonOnClick.This 内暂停该线程是我的代码 reference.Thank 你的帮助。
(如何在 buttonOnClick 中暂停线程)
int n;
TextView box2;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
String string = box2.getText().toString();
int num = Integer.parseInt(string);
num = num - 1;
string = Integer.toString(num);
box2.setText(string);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
class Allthread extends Thread { //backgroud task
@Override
public void run() {
// TODO Auto-generated method stub
for (i = 30; i > 0; i--) {
try {
Thread.sleep(1000);
//handler.sendMessage(handler.obtainMessage());
handler.sendMessage(handler.obtainMessage());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void buttonOnClick(View view) {
//do something when enter is clicked
Button enter = (Button) findViewById(R.id.button);
TextView box1 = (TextView) findViewById(R.id.box1);
n++;
box1.setText("number" + n);
if(n==1){
Allthread T1 = new Allthread();
T1.start();//start the thread
}
// I want pause thread here.
}
}
看到这个link How to pause/resume thread in Android?