服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android中断线程的处理方法

Android中断线程的处理方法

2021-03-11 15:28Android开发网 Android

这篇文章主要介绍了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程序设计有所帮助。

延伸 · 阅读

精彩推荐