Android通用多个线程通信管理框架

上传人:飞*** 文档编号:41450453 上传时间:2018-05-29 格式:DOC 页数:13 大小:58KB
返回 下载 相关 举报
Android通用多个线程通信管理框架_第1页
第1页 / 共13页
Android通用多个线程通信管理框架_第2页
第2页 / 共13页
Android通用多个线程通信管理框架_第3页
第3页 / 共13页
Android通用多个线程通信管理框架_第4页
第4页 / 共13页
Android通用多个线程通信管理框架_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《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.

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 研究报告 > 综合/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号