欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中断线程的处理方法

程序员文章站 2022-07-03 19:02:50
本文实例讲述了android中断线程的处理方法。分享给大家供大家参考。具体方法如下: 我现在对一个用户注册的功能 1.用progressdialog将当前页面设成不可操...

本文实例讲述了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程序设计有所帮助。