我先描述下我的问题:
我现在对一个用户注册的功能
1.用ProgressDialog将当前页面设成不可操作(保留返回键 退出ProgressDialog)
2.用一个线程clientThread执行数据的提交和返回
问题:考虑到ProgressDialog运行过程中,返回键的操作,应该终止clientThread的运行。
以下是我的代码,希望大家互相讨论,指出我的不足,最好能给出比较好的解决方案
好了不废话了,先贴代码!!
我还是把我btn_register的操作都贴上来好了
btn_register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(doBeforeSubmit()){ try { Log.v(TAG, "clientThread prepare01。。。:"+Thread.interrupted()); clienThread=new Thread(){ public void run() { try { Log.v(TAG, "clientThread prepare02。。。:"+Thread.interrupted()); while (!Thread.interrupted()) { Log.v(TAG, "clientThread starting。。。"); MeApiCall meApiCall=new MeApiCallImpl(); Me me=new Me(); me.setMeEmail(et_register_account.getText().toString().trim()); me.setMePwd(et_register_pwd.getText().toString().trim()); me.setMeName(et_register_nickName.getText().toString().trim()); Log.v(TAG, "clientThread data loaded。。。"); meApiCall.register(me); Log.v(TAG, "clientThread callback。。。"); handler.sendEmptyMessage(AppConfig.Register.SUCCESS_MSG); clienThread.interrupt(); //中断线程 } } catch (InterruptedException e) { // TODO: handle exception Log.v(TAG, "clientThread is interrupt"); }catch (Exception e) { // TODO Auto-generated catch block Log.v(TAG, "exception: "+e.getMessage()); handler.sendEmptyMessage(AppConfig.Register.FAIL_OTHER_MSG); e.printStackTrace(); } }; }; Log.v(TAG, "progressDialog prepare01。。。:"); progressDialog=ProgressDialog.show(Register.this, "", "正在获取....", true, true, new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // TODO Auto-generated method stub try { clienThread.interrupt(); Log.v(TAG, "interrupt is success!"); } catch (Exception e) { // TODO Auto-generated catch block Log.v(TAG, "interrupt is fail!"); e.printStackTrace(); } } } ); Log.v(TAG, "clientThread prepareing_end。。。"); clienThread.start(); //Log.v(TAG, "clientThread prepareing_end。。。"); } catch (Exception e) { // TODO Auto-generated catch block Log.v(TAG, e.getMessage()+" 00"); e.printStackTrace(); } } } });