【发布时间】:2015-08-05 13:28:52
【问题描述】:
第一次,我调用 HandlerThread.start() 来处理后台服务。在所有的东西完成后,我想通过调用 HandlerThread.quit() 来结束这个线程。
然后第二次,我启动这个Handler,检查了HandlerThread.isAlive(),isAlive()返回false,但是当我再次调用HandlerThread时,HandlerThread.start()。
但是我得到了 IllegalThreadStateException,为什么?
在我再次安全地调用 handlerThread.start() 之前,我如何才能真正停止 HandlerThread?
onCreate(){
...............
CurrentLocationPresenter =
new CurrentLocationPresenter(getApplicationContext(),mHandler);
}
public void onClick(View v) {
int id = v.getId();
switch (id){
case R.id.showplacebutton:
showPlaceInMapActivity();
break;
case R.id.showgpsbutton:
if (mCurrentLocationPresenter.isAlive()){
break;
}
mCurrentLocationPresenter.start();
break;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what == CurrentLocationPresenter.WHATCODE){
mCurrentLatlng = (LatLng) msg.obj;
mTextView.setVisibility(View.VISIBLE);
if (mCurrentLatlng!=null) {
mTextView.setText(mCurrentLatlng.toString());
}
mCurrentLocationPresenter.getLooper().quit();
}
}
};
【问题讨论】:
标签: android handler android-handlerthread