1 android handler android-handler
我已经创建了一个Android应用程序,当一个按钮点击处理程序时,将每隔2秒激活一个烤面包机.同样在另一方面,我有一个停止按钮,我想从中停止点击正在运行的线程
这是我的代码
private final int FIVE_SECONDS = 2000;
public void scheduleSendLocation() {
handler.postDelayed(new Runnable() {
public void run() {
sendLocation(); // this method will contain your almost-finished HTTP calls
handler.postDelayed(this, FIVE_SECONDS);
}
}, FIVE_SECONDS);
}
protected void sendLocation() {
Toast.makeText(getApplicationContext(), "Your Location is ", Toast.LENGTH_SHORT).show();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
stop = (Button) findViewById(R.id.stop);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
scheduleSendLocation();
}
});
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// what to write here
}
});
}
Run Code Online (Sandbox Code Playgroud)
尝试调用以下代码
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
handler.removeCallbacks(runnable);
}
});
Run Code Online (Sandbox Code Playgroud)
runnable调用postDelayed时添加的runnable 在哪里?runnable将删除消息队列中的所有待处理帖子.
| 归档时间: |
|
| 查看次数: |
5699 次 |
| 最近记录: |