Android 提供了很多執行緒的方法,在本部落格中也可以參考 Thread 與 AsyncTask,在不同的情境我們可以使用不同的方法,此篇我們介紹 Handler 如何操作 Runnable。將 Handler 想像為處理器,而 Runnable 為被處理的事件,所以一個處理器可以處理很多事件,那大略架構就會樣是:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| public class MainActivity extends Activity { Handler mHandler; TextView txtCount; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtCount = (TextView) findViewById(R.id.txtCount); mHandler = new Handler(); mHandler.post(runnable); } final Runnable runnable = new Runnable() { public void run() { // TODO Auto-generated method stub // 需要背景作的事 } }; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
| final Runnable runnable = new Runnable() { public void run() { // TODO Auto-generated method stub // 需要背景作的事 try { for ( int i = 0 ; i < 10 ; i++) { Thread.sleep( 1000 ); } } catch (Exception e) { e.printStackTrace(); } } }; |
1
2
3
4
5
6
7
8
9
10
11
| int count = 0 ; final Runnable runnable = new Runnable() { public void run() { // TODO Auto-generated method stub // 需要背景作的事 if (count < 10 ) { count++; mHandler.postDelayed(runnable, 1000 ); } } }; |
1
2
3
4
5
6
7
8
9
10
11
12
| int count = 0 ; final Runnable runnable = new Runnable() { public void run() { // TODO Auto-generated method stub // 需要背景作的事 if (count < 10 ) { txtCount.setText(Integer.toString(count+ 1 )); count++; mHandler.postDelayed(runnable, 1000 ); } } }; |
1
2
3
4
5
6
7
8
| @Override protected void onPause() { // TODO Auto-generated method stub super .onPause(); if (mHandler != null ) { mHandler.removeCallbacks(runnable); } } |
沒有留言:
張貼留言