Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中斷線程的處理方法

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程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved