Android中断线程的处理方法

作者:shichen2014 时间:2023-07-31 11:51:55 

本文实例讲述了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,线程
0
投稿

猜你喜欢

  • Jexcel实现按一定规则分割excel文件的方法

    2023-06-29 07:52:01
  • Java中双大括号初始化的理解与使用

    2023-08-30 08:44:55
  • 使用Feign传递请求头信息(Finchley版本)

    2023-06-07 22:38:07
  • C#实现自定义双击事件

    2023-05-01 16:31:57
  • java贪吃蛇游戏编写代码

    2023-06-16 02:41:10
  • Java设计模式之装饰者模式详解

    2021-08-16 03:00:17
  • Java Spring Controller 获取请求参数的几种方法详解

    2023-04-07 02:11:17
  • java递归菜单树转换成pojo对象

    2022-08-12 04:04:40
  • Java简单使用redis-zset实现排行榜

    2021-06-07 20:34:54
  • Android通过json向MySQL中读写数据的方法详解【写入篇】

    2022-07-20 19:12:39
  • 深入解析java中的locale

    2023-11-09 18:14:20
  • Java实现的两个线程同时运行案例

    2023-05-14 09:10:40
  • Spring Cloud升级最新Finchley版本的所有坑

    2021-09-02 07:21:51
  • Springboot @Validated和@Valid的区别及使用详解

    2023-05-30 18:40:25
  • Android自定义圆环倒计时控件

    2023-10-07 06:52:41
  • Android实现串口通信

    2023-03-06 18:13:10
  • iOS WebView中使用webp格式图片的方法

    2023-06-17 22:06:03
  • Java实现分布式系统限流

    2022-05-31 22:38:05
  • c#入门之分支语句使用方法(三元运算符、if语句、switch语句)

    2021-12-06 00:55:20
  • Android实现文字下方加横线

    2022-11-29 06:31:06
  • asp之家 软件编程 m.aspxhome.com