Android 中断线程的处理

http://www.oschina.net/code/snippet_163910_6185


我现在对一个用户注册的功能
1.用ProgressDialog将当前页面设成不可操作(保留返回键 退出ProgressDialog)
2.用一个线程clientThread执行数据的提交和返回

问题:考虑到ProgressDialog运行过程中,返回键的操作,应该终止clientThread的运行。

以下是我的代码,希望大家互相讨论,指出我的不足,最好能给出比较好的解决方案


btn_register.setOnClickListener(new View.OnClickListener() { 
02	       
03	    @Override 
04	    public void onClick(View v) { 
05	        // TODO Auto-generated method stub  
06	        if(doBeforeSubmit()){ 
07	       
08	        try { 
09	            Log.v(TAG, "clientThread prepare01。。。:"+Thread.interrupted()); 
10	            clienThread=new Thread(){ 
11	            public void run() { 
12	                try { 
13	                Log.v(TAG, "clientThread prepare02。。。:"+Thread.interrupted()); 
14	                while (!Thread.interrupted()) { 
15	                    Log.v(TAG, "clientThread starting。。。"); 
16	                    MeApiCall meApiCall=new MeApiCallImpl(); 
17	                    Me me=new Me(); 
18	                    me.setMeEmail(et_register_account.getText().toString().trim()); 
19	                    me.setMePwd(et_register_pwd.getText().toString().trim()); 
20	                    me.setMeName(et_register_nickName.getText().toString().trim());                                      
21	                       
22	                    Log.v(TAG, "clientThread data loaded。。。");                                       
23	                    meApiCall.register(me); 
24	                    Log.v(TAG, "clientThread callback。。。"); 
25	                   
26	                    handler.sendEmptyMessage(AppConfig.Register.SUCCESS_MSG); 
27	                   
28	                       
29	                   clienThread.interrupt();   //中断线程  
30	                } 
31	                } catch (InterruptedException e) { 
32	                // TODO: handle exception  
33	                Log.v(TAG, "clientThread is interrupt"); 
34	                }catch (Exception e) { 
35	                // TODO Auto-generated catch block  
36	                Log.v(TAG, "exception: "+e.getMessage()); 
37	                handler.sendEmptyMessage(AppConfig.Register.FAIL_OTHER_MSG); 
38	                e.printStackTrace(); 
39	                } 
40	            }; 
41	 
42	            }; 
43	            Log.v(TAG, "progressDialog prepare01。。。:"); 
44	            progressDialog=ProgressDialog.show(Register.this,  
45	                               "",  
46	                               "正在获取....",  
47	                               true,  
48	                               true, 
49	                               new DialogInterface.OnCancelListener() { 
50	                               
51	                            @Override 
52	                            public void onCancel(DialogInterface dialog) { 
53	                                // TODO Auto-generated method stub  
54	                                   
55	                                try { 
56	                                    clienThread.interrupt(); 
57	                                    Log.v(TAG, "interrupt is success!"); 
58	                                } catch (Exception e) { 
59	                                    // TODO Auto-generated catch block  
60	                                    Log.v(TAG, "interrupt is fail!"); 
61	                                    e.printStackTrace(); 
62	                                } 
63	                               
64	                            } 
65	                            } 
66	                              ); 
67	            Log.v(TAG, "clientThread prepareing_end。。。"); 
68	            clienThread.start(); 
69	            //Log.v(TAG, "clientThread prepareing_end。。。");  
70	        } catch (Exception e) { 
71	            // TODO Auto-generated catch block  
72	            Log.v(TAG, e.getMessage()+" 00"); 
73	            e.printStackTrace(); 
74	        } 
75	           
76	           
77	        } 
78	           
79	    } 
80	    });


你可能感兴趣的:(thread,android,exception,dialog,callback)