• 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 异步加载图片大家可以看安卓教程网吧:http://android.662p.com/thread-316-1-1.html

  • 相关阅读:
    js事件之event.preventDefault()与event.stopPropagation()用法区别
    [转] The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing
    [转] Visual Studio Code behind a proxy
    [转] js == 与 === 的区别
    [转]说说C#的async和await
    [转]Sublime Text3注册码(可用)
    Oracle 12c
    SQL Server死锁
    Initialize the Storage Emulator by Using the Command-Line Tool
    Microsoft Fakes
  • 原文地址:https://www.cnblogs.com/huasili/p/3422763.html
Copyright © 2020-2023  润新知