gpt4 book ai didi

Android中断线程的处理方法

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章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程序设计有所帮助.

最后此篇关于Android中断线程的处理方法的文章就讲到这里了,如果你想了解更多关于Android中断线程的处理方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com