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

Android线程间通信的Message机制

 2010-06-01 15:44:00 来源:WEB开发网   
核心提示:83. //implement the OnClickListener interface84. @Override85. public void onClick(View v) {86. switch(v.getId()){87. case 101:88. //主线程发送消息给自己89. Looper looper;

83. //implement the OnClickListener interface

84. @Override

85. public void onClick(View v) {

86. switch(v.getId()){

87. case 101:

88. //主线程发送消息给自己

89. Looper looper;

90. looper = Looper.myLooper(); //get the Main looper related with the main thread

91. //如果不给任何参数的话会用当前线程对应的Looper(这里就是Main Looper)为Handler里面的成员mLooper赋值

92. mHandler = new EventHandler(looper);

93. //mHandler = new EventHandler();

94. // 清除整个MessageQueue里的消息

95. mHandler.removeMessages(0);

96. String obj = "This main thread's message and received by itself!";

97. //得到Message对象

98. Message m = mHandler.obtainMessage(1, 1, 1, obj);

99. // 将Message对象送入到main thread的MessageQueue里面

100. mHandler.sendMessage(m);

101. break;

102. case 102:

103. //other线程发送消息给主线程

104. postRunnable = false;

105. noLooerThread = new NoLooperThread();

106. noLooerThread.start();

107. break;

108. case 103:

109. //other thread获取它自己发送的消息

110. tv.setText("please look at the error level log for other thread received message");

111. ownLooperThread = new OwnLooperThread();

112. ownLooperThread.start();

113. break;

114. case 104:

115. //other thread通过Post Runnable方式发送消息给主线程

116. postRunnable = true;

117. noLooerThread = new NoLooperThread();

118. noLooerThread.start();

119. break;

120. case 105:

121. //主线程发送消息给other thread

122. if(null!=mOtherThreadHandler){

123. tv.setText("please look at the error level log for other thread received message from main thread");

上一页  3 4 5 6 7 8 9 10 11 12 13  下一页

Tags:Android 线程 通信

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