如何停止处理程序线程?
How to Stop Handler thread?
我想在单击按钮时停止此线程。
TIME_OUT = 45000;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new
Intent(MapsActivity.this,MapsActivity.class);
startActivity(i);
finish();
}
}, TIME_OUT);
我在 Activity 的 onCreate
中使用上述处理程序。我想阻止它。如何在单击任何按钮或单击“返回”时停止此线程?
Handler handler = new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new
Intent(MapsActivity.this,MapsActivity.class);
startActivity(i);
finish();
}
}, TIME_OUT);
然后您可以使用 Handler#removeCallbacksAndMessages 删除此回调或任何回调。
handler.removeCallbacksAndMessages(null);
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
Intent i = new
Intent(MapsActivity.this,MapsActivity.class);
startActivity(i);
finish();
}
};
handler.post(runnable);
// use this when you don't want callback to be called anymore
handler.removeCallbacks(runnable);
好的,最好的选择是像那样使用布尔值作为标志
TIME_OUT = 45000;
//add this boolean
boolean run =true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//run this method only when run is true
if(run==true){
//your code
}
}
}, TIME_OUT);
//on button click just change the boolean to flag and it will stop the run method
//on click
run=false;
public class MainActivity extends Activity{
Handler handler;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
Intent i = new
Intent(MapsActivity.this,MapsActivity.class);
startActivity(i);
finish();
}
};
handler.post(runnable);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
handler.removeCallbacks(runnable);
}
});
}
}
我想在单击按钮时停止此线程。
TIME_OUT = 45000;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new
Intent(MapsActivity.this,MapsActivity.class);
startActivity(i);
finish();
}
}, TIME_OUT);
我在 Activity 的 onCreate
中使用上述处理程序。我想阻止它。如何在单击任何按钮或单击“返回”时停止此线程?
Handler handler = new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new
Intent(MapsActivity.this,MapsActivity.class);
startActivity(i);
finish();
}
}, TIME_OUT);
然后您可以使用 Handler#removeCallbacksAndMessages 删除此回调或任何回调。
handler.removeCallbacksAndMessages(null);
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
Intent i = new
Intent(MapsActivity.this,MapsActivity.class);
startActivity(i);
finish();
}
};
handler.post(runnable);
// use this when you don't want callback to be called anymore
handler.removeCallbacks(runnable);
好的,最好的选择是像那样使用布尔值作为标志
TIME_OUT = 45000;
//add this boolean
boolean run =true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//run this method only when run is true
if(run==true){
//your code
}
}
}, TIME_OUT);
//on button click just change the boolean to flag and it will stop the run method
//on click
run=false;
public class MainActivity extends Activity{
Handler handler;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
Intent i = new
Intent(MapsActivity.this,MapsActivity.class);
startActivity(i);
finish();
}
};
handler.post(runnable);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
handler.removeCallbacks(runnable);
}
});
}
}