package com.example.threadcomm;

import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
/** Called when the activity is first created. */
// 主线程给子线程发送消息
Button verifyButton;
TextView textView;

private static int FROM_MAIN_MSG1 = 1;
private static int FROM_SUB_MSG2 = 11;

// 主线程
MainActivityHandler mainHandler;
// 其它线程
SubThreadHandler subThreadHandler;

private int i = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

verifyButton = (Button) findViewById(R.id.verify_button);
textView = (TextView) findViewById(R.id.text);

textView.setText("");

verifyButton.setOnClickListener(clickListener);

// 创建子线程
MyThread thread = new MyThread();
thread.start();
}

OnClickListener clickListener = new OnClickListener() {

@Override
public void onClick(View v) {
switch (v.getId()) {
// 主线程给子线程发送消息
case R.id.verify_button:
textView.append("UI main thread: shall send message to sub-thread......\n\n");
subThreadHandler.sendEmptyMessage(1);
break;

default:
break;
}
}
};

// 主线程处理消息

class MainActivityHandler extends Handler {

public MainActivityHandler(Looper looper) {
super(looper);
}

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);

if (msg.what == 11) {
textView.append("UI main thread: received message from sub-thread......\n\n");
}
}
}

// 以下是子线程类和他的handler类
class MyThread extends Thread {
@Override
public void run() {
Log.e("threads_comm", "sub thread was created here......");

super.run();
// 创建该线程的Looper对象,用于接收消息
Looper.prepare();
// 线程的looper创建的handler表示消息接收者是子线程
subThreadHandler = new SubThreadHandler(Looper.myLooper());
// 循环从MessageQueue中取消息。
Looper.loop();
}
}

// 子线程的Handler

class SubThreadHandler extends Handler {
public SubThreadHandler(Looper looper) {
super(looper);
}

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);

if (msg.what == FROM_MAIN_MSG1) {
mainHandler = new MainActivityHandler(Looper.getMainLooper());
textView.append("Sub-thread: received message from UI main thread, and will send message to UI main thread......\n\n");
mainHandler.sendEmptyMessage(11);

}
}
}
}



对应的activity_main.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_above="@+id/button3"
android:layout_marginBottom="50dp"
/>

<Button
android:id="@+id/verify_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"
android:layout_centerInParent="true"
android:background="@drawable/shape"
/>
</RelativeLayout>


更多相关文章

  1. Android(安卓)AsyncTask
  2. Android的线程和线程池
  3. Android音频系统之音量控制详解(Android(安卓)5.1)
  4. Android(安卓)AsyncTask异步线程
  5. Android的线程和线程池
  6. Android客户消费ActiveMQ消息以及消息持久化
  7. Android(安卓)线程
  8. android核心技术之ANR分析(MTK)
  9. 解析Android的 消息传递机制Handler

随机推荐

  1. [Android]滑动刷新ListView——android-p
  2. android - TextView单行显示...或者文字
  3. Android(安卓)RIL学习
  4. Android(安卓)Gradle编译学习日记之一(搭
  5. 【Android學習專題】網絡通信篇:Socket TC
  6. android 控件属性大全
  7. RelativeLayout的各种属性整理
  8. Android菜鸟的成长笔记(11)——Android中的
  9. android之layout布局和ListView中的一些
  10. Android 目录