作者: blogercn (1 篇文章) 日期: 五月 7, 2012 在 6:21 下午
android的应用程序支持多线程,多线程编程为我们充分利用系统资源提供了便利,同时也为设计复杂UI和耗时操作提供了途径,提升了安卓用户的使用体验。Android的多线程和JAVA没有多大变化,唯一的变化大概在于无法直接使用CANVAS修改屏幕元素,当然安卓为我们提供了surfaceview类来实现多线程中通过画布canvas修改屏幕。这为设计UI和开发游戏带来了方便。因此,研究和使用多线程编程,对我们深入学习安卓编程有着十分重要的作用。
线程的方法比较多,常用的有:
start();
run();
sleep();
stop();
destroy();
join();
suspend();
resume();
yield(); wait(); notify();
线程启动一定要使用start方法,线程操作使用run方法,线程休眠使用sleep方法,线程停止使用stop,线程销毁使用destroy方法,线程同步使用JOIN方法,前面三种最常用,一般来说这三种就可以满足大部分线程使用需求,run结束时线程自动死亡,stop,destroy虽然也能停止线程,但不推荐使用,,前者会产生异常,后者是强制终止,不会释放锁,一般会在RUN里设置一个状态信号来等其自动结束,这里使用volatile boolean bThreadRun。后面是暂停,继续,挂起,由于会产生死锁问题,很少使用。大部分情况会使用wait和notify替代;
这里我使用一个线程来计算变量并更新窗口标题。主要代码如下:使用eclipse创建一个项目。为Activity添加onStart,onPause, onStop等方法,Activity是我们最常使用的一个类,也是android的核心类,为应用程序管理并显示一个屏幕,开发的人不应该对其陌生。Activity活动主要的方法有,onCreate,onStart,onStop,onPause,onResume, onRestart,onDestroy,onRestoreInstanceState,onSaveInstanceState,一般的执行顺序是,onCreate,onStart,onResume,当窗口不是最顶层时,执行onPause,onstop,为顶层时,执行onRestart,onResume,一直循环,直到onDestroy.如果保存窗口,重载onSaveInstanceState,并在进入时重载onRestoreInstanceState。这里我在Activity的方法onStart里创建线程:
01.MyThread myThread = new MyThread(); 02. myThread.start();
在下面添加MyThread的实现代码:
01.public class MyThread extends Thread { 02. // 声明字符串变量 03. public MyThread() { 04. } 05. 06. @Override 07. public void start() { 08. super.start(); 09. } 10. 11. // 线程的主要工作方法 12. @Override 13. public void run() { 14. while (true) { 15. try { 16. sleep(5000); 17. if (c > ((1 << 31) - 1)) { 18. c = 0; 19. } else { 20. c++; 21. } 22. Message message = new Message(); 23. message.what = 1; 24. mHandler.sendMessage(message); 25. } catch (InterruptedException ex) { 26. } 27. } 28. } 29. 30. }
实现updatetitle方法:
01.public void updateTitle() { 02. setTitle("test thread " + c); 03. }
这里没有使用updatetitle直接更新窗口,而是使用handler,这是因为直接使用是不安全的,会造成线程重启等问题。Android 引进了Handler 这个特殊的类,用它作为Runnable和Activity交互的桥梁,所以我们只能在run方法中发送Message,而在Handler里,通过不同的Message执行不同的任务。为程序添加handler;
01.private Handler mHandler = new Handler() { 02. public void handleMessage(Message msg) { 03. switch (msg.what) { 04. case 1: 05. updateTitle(); 06. break; 07. } 08. }; 09. };
最后完整的代码如下,使用线程处理数据,并把数据显示在窗口上:
01.package com.test; 02. 03.import android.app.Activity; 04.import android.os.Bundle; 05.import java.lang.Thread; 06.import android.os.Message; 07.import android.os.Handler; 08.import android.graphics.Color; 09. 10.public class TestThreadActivity extends Activity { 11. int c = Color.BLUE; 12. MyThread myThread; 13. volatile boolean bThreadRun = false; 14. 15. /** Called when the activity is first created. */ 16. @Override 17. public void onCreate(Bundle savedInstanceState) { 18. super.onCreate(savedInstanceState); 19. setContentView(R.layout.main); 20. } 21. 22. @Override 23. protected void onRestoreInstanceState(android.os.Bundle savedInstanceState) { 24. super.onRestoreInstanceState(savedInstanceState); 25. } 26. 27. @Override 28. protected void onSaveInstanceState(android.os.Bundle outState) { 29. super.onSaveInstanceState(outState); 30. } 31. 32. @Override 33. protected void onStart() { 34. super.onStart(); 35. myThread = new MyThread(); 36. myThread.start(); 37. bThreadRun = true; 38. } 39. 40. @Override 41. protected void onRestart() { 42. super.onRestart(); 43. } 44. 45. @Override 46. protected void onResume() { 47. super.onResume(); 48. } 49. 50. @Override 51. protected void onPause() { 52. super.onPause(); 53. bThreadRun = false; 54. // myThread.stop(); 55. } 56. 57. @Override 58. protected void onStop() { 59. super.onStop(); 60. onPause(); 61. } 62. 63. @Override 64. protected void onDestroy() { 65. super.onDestroy(); 66. // myThread.destroy(); 67. } 68. 69. private Handler mHandler = new Handler() { 70. public void handleMessage(Message msg) { 71. switch (msg.what) { 72. case 1: 73. updateTitle(); 74. break; 75. } 76. }; 77. }; 78. 79. public void updateTitle() { 80. setTitle("test thread " + c); 81. setTitleColor(c); 82. } 83. 84. public class MyThread extends Thread { 85. // 声明字符串变量 86. public MyThread() { 87. } 88. 89. @Override 90. public void start() { 91. super.start(); 92. } 93. 94. // 线程的主要工作方法 95. @Override 96. public void run() { 97. while (bThreadRun) { 98. try { 99. sleep(100); 100. if (c > ((1 << 16) - 1)) { 101. c = 0; 102. } else { 103. c += 100; 104. } 105. Message message = new Message(); 106. message.what = 1; 107. mHandler.sendMessage(message); 108. } catch (InterruptedException ex) { 109. } 110. } 111. } 112. 113. } 114. 115.}
线程还有另外一种常用的方法是编写Runnable接口,这里对代码做一些修改,使用线程实现对主窗口重绘,全部代码如下:
01.package com.test; 02. 03.import android.app.Activity; 04.import android.content.Context; 05.import android.os.Bundle; 06.import java.lang.Thread; 07.import android.view.View; 08.import android.graphics.Canvas; 09.import android.graphics.Color; 10.import android.graphics.Paint; 11.import android.graphics.Paint.Style; 12.import android.graphics.Rect; 13. 14.public class TestThreadActivity extends Activity { 15. int c = Color.BLUE; 16. MyThread myThread; 17. volatile boolean bThreadRun = false; 18. MyView mv; 19. 20. /** Called when the activity is first created. */ 21. @Override 22. public void onCreate(Bundle savedInstanceState) { 23. super.onCreate(savedInstanceState); 24. // setContentView(R.layout.main); 25. mv = new MyView(this); 26. setContentView(mv); 27. } 28. 29. public class MyView extends View { 30. MyView(Context context) { 31. super(context); 32. } 33. 34. @Override 35. protected void onDraw(Canvas canvas) { 36. // TODO Auto-generated method stub 37. super.onDraw(canvas); 38. 39. // 首先定义一个paint 40. Paint paint = new Paint(); 41. 42. // 绘制矩形区域-实心矩形 43. // 设置颜色 44. paint.setColor(c); 45. // 设置样式-填充 46. paint.setStyle(Style.FILL); 47. // 绘制一个矩形 48. canvas.drawRect(new Rect(0, 0, getWidth(), getHeight()), paint); 49. } 50. 51. } 52. 53. @Override 54. protected void onRestoreInstanceState(android.os.Bundle savedInstanceState) { 55. super.onRestoreInstanceState(savedInstanceState); 56. } 57. 58. @Override 59. protected void onSaveInstanceState(android.os.Bundle outState) { 60. super.onSaveInstanceState(outState); 61. } 62. 63. @Override 64. protected void onStart() { 65. super.onStart(); 66. //myThread = new MyThread(); 67. //myThread.start(); 68. new Thread (new MyThread()).start(); 69. bThreadRun = true; 70. } 71. 72. @Override 73. protected void onRestart() { 74. super.onRestart(); 75. } 76. 77. @Override 78. protected void onResume() { 79. super.onResume(); 80. } 81. 82. @Override 83. protected void onPause() { 84. super.onPause(); 85. bThreadRun = false; 86. // myThread.stop(); 87. } 88. 89. @Override 90. protected void onStop() { 91. super.onStop(); 92. onPause(); 93. } 94. 95. @Override 96. protected void onDestroy() { 97. super.onDestroy(); 98. // myThread.destroy(); 99. } 100. 101. public class MyThread implements Runnable{ 102. // 线程的主要工作方法 103. @Override 104. public void run() { 105. while (bThreadRun) { 106. try { 107. Thread.sleep(500); 108. if (c > ((1 << 31) - 1)) { 109. c = 0; 110. } else { 111. c += 100; 112. } 113. mv.postInvalidate(); 114. } catch(InterruptedException e){ 115. } 116. } 117. } 118. 119. } 120. 121.}
|