handler.removeCallbacks(runable);无法停止线程(见红色部分)

【声明】此文转载自:http://www.bubuko.com/infodetail-669650.html

——尊重作者,知识无价,交流无限!



一、先看图片,一目了然:



二、再看代码,了然于胸:

Activity代码:

[java]  view plain  copy
  1. package com.example.mydemo;  
  2.  2   
  3.  3 import android.app.Activity;  
  4.  4 import android.os.Bundle;  
  5.  5 import android.os.Handler;  
  6.  6 import android.os.Message;  
  7.  7 import android.view.View;  
  8.  8 import android.view.View.OnClickListener;  
  9.  9 import android.widget.Button;  
  10. 10 import android.widget.TextView;  
  11. 11   
  12. 12 public class MainActivity extends Activity {  
  13. 13     private TextView text_view = null;  
  14. 14     private Button start = null;  
  15. 15     private Button end = null;  
  16. 16     private int count = 0;  
  17. 17     // 使用handler时首先要创建一个handler  
  18. 18     Handler handler = new Handler();  
  19. 19     // 要用handler来处理多线程可以使用runnable接口,这里先定义该接口  
  20. 20     // 线程中运行该接口的run函数  
  21. 21     Runnable update_thread = new Runnable() {  
  22. 22         public void run() {  
  23. 23             // 线程每次执行时输出"UpdateThread..."文字,且自动换行  
  24. 24             // textview的append功能和Qt中的append类似,不会覆盖前面  
  25. 25             // 的内容,只是Qt中的append默认是自动换行模式  
  26. 26             // text_view.append("\nUpdateThread...");  
  27. 27             text_view.setText(String.valueOf(count++));  
  28. 28             // 延时1s后又将线程加入到线程队列中  
  29. 29             if (count >= 5) {  
  30. 30                 // handler.removeCallbacks(update_thread);//用此行停止不行,需要发消息外部停止才可以。不明白?  
  31. 31                 Message message = new Message();  
  32. 32                 message.what = 1;  
  33. 33                 handlerStop.sendMessage(message);  
  34. 34             }  
  35. 35             handler.postDelayed(update_thread, 1000);  
  36. 36   
  37. 37         }  
  38. 38     };  
  39. 39   
  40. 40     @Override  
  41. 41     public void onCreate(Bundle savedInstanceState) {  
  42. 42         super.onCreate(savedInstanceState);  
  43. 43         setContentView(R.layout.activity_main);  
  44. 44   
  45. 45         text_view = (TextView) findViewById(R.id.text_view);  
  46. 46         start = (Button) findViewById(R.id.start);  
  47. 47         start.setOnClickListener(new StartClickListener());  
  48. 48         end = (Button) findViewById(R.id.end);  
  49. 49         end.setOnClickListener(new EndClickListener());  
  50. 50         handler.post(update_thread);  
  51. 51     }  
  52. 52   
  53. 53     private class StartClickListener implements OnClickListener {  
  54. 54         public void onClick(View v) {  
  55. 55             // TODO Auto-generated method stub  
  56. 56             // 将线程接口立刻送到线程队列中  
  57. 57             handler.post(update_thread);  
  58. 58         }  
  59. 59     }  
  60. 60   
  61. 61     private class EndClickListener implements OnClickListener {  
  62. 62         public void onClick(View v) {  
  63. 63             // TODO Auto-generated method stub  
  64. 64             // 将接口从线程队列中移除  
  65. 65             count = 0;  
  66. 66             handler.removeCallbacks(update_thread);  
  67. 67         }  
  68. 68     }  
  69. 69   
  70. 70     final Handler handlerStop = new Handler() {  
  71. 71         public void handleMessage(Message msg) {  
  72. 72             switch (msg.what) {  
  73. 73             case 1:  
  74. 74                 count = 0;  
  75. 75                 handler.removeCallbacks(update_thread);  
  76. 76                 break;  
  77. 77             }  
  78. 78             super.handleMessage(msg);  
  79. 79         }  
  80. 80   
  81. 81     };  
  82. 82 }  


Xml文件

[java]  view plain  copy
  1.  1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.  2     xmlns:tools="http://schemas.android.com/tools"  
  3.  3     android:layout_width="match_parent"  
  4.  4     android:layout_height="match_parent"  
  5.  5     android:orientation="vertical" >  
  6.  6   
  7.  7     <TextView  
  8.  8         android:id="@+id/text_view"  
  9.  9         android:layout_width="fill_parent"  
  10. 10         android:layout_height="200dip"  
  11. 11         android:text="@string/hello_world"  
  12. 12         tools:context=".MainActivity" />  
  13. 13   
  14. 14     <Button  
  15. 15         android:id="@+id/start"  
  16. 16         android:layout_width="fill_parent"  
  17. 17         android:layout_height="wrap_content"  
  18. 18         android:text="start" />  
  19. 19   
  20. 20     <Button  
  21. 21         android:id="@+id/end"  
  22. 22         android:layout_width="fill_parent"  
  23. 23         android:layout_height="wrap_content"  
  24. 24         android:text="end" />  
  25. 25   
  26. 26 </LinearLayout>  

猜你喜欢

转载自blog.csdn.net/taowuhua0505/article/details/79933623
今日推荐