龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 移动开发 > Android开发 >

Android中断线程的处理方法

时间:2014-11-14 11:41来源:网络整理 作者:网络 点击:
分享到:
这篇文章主要介绍了Android中断线程的处理方法,涉及到线程的中断、处理与返回等操作,具有一定的参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android中断线程的处理方法。分享给大家供大家参考。具体方法如下:

我现在对一个用户注册的功能
1.用ProgressDialog将当前页面设成不可操作(保留返回键 退出ProgressDialog)
2.用一个线程clientThread执行数据的提交和返回

问题:考虑到ProgressDialog运行过程中,返回键的操作,应该终止clientThread的运行。

代码如下,感兴趣的朋友可以参考运行一下,以便得出更好的解决办法。

复制代码 代码如下:

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(); 
                } 
            } 
        } 
    }); 

希望本文所述对大家的Android程序设计有所帮助。

精彩图集

赞助商链接