开发学院手机开发Android 开发 Android线程间通信的Message机制 阅读

Android线程间通信的Message机制

 2010-06-01 15:44:00 来源:WEB开发网   
核心提示:executed in main thread!";175. }176. else {177. mNoLooperThreadHandler = new EventHandler(myLooper);178. obj = "This is from NoLooperThread self and h
executed in main thread!";

175. }

176. else {

177. mNoLooperThreadHandler = new EventHandler(myLooper);

178. obj = "This is from NoLooperThread self and handleMessage function executed in NoLooperThread!";

179. }

180. mNoLooperThreadHandler.removeMessages(0);

181. if(false == postRunnable){

182. //send message to main thread

183. Message m = mNoLooperThreadHandler.obtainMessage(2, 1, 1, obj);

184. mNoLooperThreadHandler.sendMessage(m);

185. Log.e(sTag, "NoLooperThread id:" + this.getId());

186. }else{

187. //下面new出来的实现了Runnable接口的对象中run函数是在Main Thread中执行,不是在NoLooperThread中执行

188. //注意Runnable是一个接口,它里面的run函数被执行时不会再新建一个线程

189. //您可以在run上加断点然后在eclipse调试中看它在哪个线程中执行

190. mNoLooperThreadHandler.post(new Runnable(){

191. @Override

192. public void run() {

193. tv.setText("update UI through handler post runnalbe mechanism!");

194. noLooerThread.stop();

195. }

196. });

197. }

198. }

199. }

200.

201. //OwnLooperThread has his own message queue by execute Looper.prepare();

202. class OwnLooperThread extends Thread{

203. private EventHandler mOwnLooperThreadHandler;

204. public void run() {

205. Looper.prepare();

206. Looper myLooper, mainLooper;

207. myLooper = Looper.myLooper();

208. mainLooper = Looper.getMainLooper(); //这是一个static函数

209. String obj;

210. if(myLooper == null){

211. mOwnLooperThreadHandler = new EventHandler(mainLooper);

212. obj = "OwnLooperThread has no looper and handleMessage function executed in main thread!";

上一页  5 6 7 8 9 10 11 12 13 14 15  下一页

Tags:Android 线程 通信

编辑录入:coldstar [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接