《Android通用多个线程通信管理框架》由会员分享,可在线阅读,更多相关《Android通用多个线程通信管理框架(13页珍藏版)》请在金锄头文库上搜索。
1、Android 通用多个线程通信管理框架通用多个线程通信管理框架-学习笔记学习笔记通用多个线程通信管理框架: 1、Handler 监听者框架:子线程是事件源,主线程是监听者。Handler 作为子线程的监听器出现:主线程中生成 Handler 的子类,并重写 handleMessage(Message msg) 方法,用来对子线程响应。子线程调用 Hanlder 的 sendMessage(message)发送事件。 view plaincopy to clipboardprint? package fy.test; import android.app.Activity; import an
2、droid.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.TextView; public class MyTest extends Activity public static final int REFRESH = 0x000001; private TextView text = null; private int i = 0; private Handler mHandler = null; /* Called when the activity is fir
3、st created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); text = (TextView) findViewById(R.id.threadText); mHandler = new Handler() Override public void handleMessage(Message msg) if (msg.what = REFRESH) text.setText(i
4、+ “); super.handleMessage(msg); ; new MyThread().start(); public class MyThread extends Thread public void run() while (!Thread.currentThread().isInterrupted() i+; Message msg = new Message(); msg.what = REFRESH; mHandler.sendMessage(msg); try Thread.sleep(1000); catch (InterruptedException e) e.pri
5、ntStackTrace(); package fy.test; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.TextView; public class MyTest extends Activity public static final int REFRESH = 0x000001; private TextView text = null; private int i =
6、 0; private Handler mHandler = null; /* Called when the activity is first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);text = (TextView) findViewById(R.id.threadText);mHandler = new Handler() Overridepublic void
7、 handleMessage(Message msg) if (msg.what = REFRESH) text.setText(i + “);super.handleMessage(msg);new MyThread().start(); public class MyThread extends Thread public void run() while (!Thread.currentThread().isInterrupted() i+;Message msg = new Message();msg.what = REFRESH;mHandler.sendMessage(msg);t
8、ry Thread.sleep(1000); catch (InterruptedException e) e.printStackTrace(); 2、Handler 钩子方式:在主线程中生成一个 Hanlder,用 Handler 的 Post(Runnable)方法可 以将 Runnable 钩到主线程中运行。 view plaincopy to clipboardprint? package fy.test; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import
9、android.widget.TextView; public class MyTest extends Activity private TextView text = null; private int i = 0; private Handler mHandler = null; /* Called when the activity is first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView
10、(R.layout.main); text = (TextView) findViewById(R.id.threadText); mHandler = new Handler(); new Thread(new Runnable() Override public void run() while (true) i+; mHandler.post(new Runnable() Override public void run() text.setText(i + “); ); try Thread.sleep(1000); catch (InterruptedException e) e.p
11、rintStackTrace(); ).start(); package fy.test; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class MyTest extends Activity private TextView text = null; private int i = 0; private Handler mHandler = null; /* Called when the ac
12、tivity is first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);text = (TextView) findViewById(R.id.threadText);mHandler = new Handler();new Thread(new Runnable() Overridepublic void run() while (true) i+;mHandler.
13、post(new Runnable() Overridepublic void run() text.setText(i + “););try Thread.sleep(1000); catch (InterruptedException e) e.printStackTrace();).start(); 注意一下代码是错误的,因为 post 里的线程结束后才会刷新 UIview plaincopy to clipboardprint? package fy.test; import android.app.Activity; import android.os.Bundle; import
14、android.os.Handler; import android.widget.TextView; public class MyTest extends Activity private TextView text = null; private int i = 0; private Handler mHandler = null; /* Called when the activity is first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedIns
15、tanceState); setContentView(R.layout.main); text = (TextView) findViewById(R.id.threadText); mHandler = new Handler(); mHandler.post(new Runnable() Override public void run() / TODO Auto-generated method stub while(true) i +; text.setText(“+i); try Thread.sleep(1000); catch (InterruptedException e) e.printStackTrace(); ); package fy.test; import android.app.Activity; import android.os.