package com.example.handler;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
public static final String TAG = "HandlerTest";
public HandlerThread mHandlerThread;
public Handler mChileHandler;
public Handler mHandlerCToP = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int id = (int) Thread.currentThread().getId();
Log.d(TAG, "mHandlerCToP currentThread id is:" + id);
}
};
public Button mButtonPtoC;
public Button mButtonCtoP;
public Button mButtonCtoC;
public Handler mHandler = new Handler();
public Handler mHandlerCtoC = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int id = (int) Thread.currentThread().getId();
Log.d(TAG, "onCreate currentThread id is:" + id);
initUIAndThread();
}
public void initUIAndThread() {
mHandlerThread = new HandlerThread("chenyu");
mHandlerThread.start();
mHandler = new Handler(mHandlerThread.getLooper()) {
@Override
public void handleMessage(Message msg) {
int id = (int) Thread.currentThread().getId();
Log.d(TAG, "initThread() mHandler handleMessage currentThread id is:" + id);
switch(msg.what) {
case 0:
mHandlerCToP.post(new Runnable(){
@Override
public void run() {
int id = (int) Thread.currentThread().getId();
Log.d(TAG, "initThread() mHandlerCToP post currentThread id is:" + id);
mButtonPtoC.setText("chenyu");
}
});
break;
case 1:
mHandlerCToP.post(new Runnable(){
@Override
public void run() {
int id = (int) Thread.currentThread().getId();
Log.d(TAG, "initThread() mHandlerCToP post currentThread id is:" + id);
mButtonCtoC.setText("chenyu");
}
});
default:
break;
}
}
};
mButtonPtoC = (Button)findViewById(R.id.button1);
mButtonCtoP = (Button)findViewById(R.id.button2);
mButtonCtoC = (Button)findViewById(R.id.button3);
mButtonPtoC.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
int id = (int) Thread.currentThread().getId();
Log.d(TAG, "mButtonPtoC currentThread id is:" + id);
Log.d(TAG, "mHandlerPToc msg.what is 0");
mHandler.sendEmptyMessage(0);
}
});
mButtonCtoP.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
int id = (int) Thread.currentThread().getId();
Log.d(TAG, "mButtonCtoP currentThread id is:" + id);
Log.d(TAG, "mHandlerPToc msg.what is 0");
mHandlerCToP.sendEmptyMessage(0);
}
});
mButtonCtoC.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
new Thread(new Runnable(){
@Override
public void run() {
int id = (int) Thread.currentThread().getId();
Log.d(TAG, "mButtonCtoC currentThread id is:" + id);
Log.d(TAG, "mHandlerCToc msg.what is 1");
mHandler.sendEmptyMessage(1);
}
}).start();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mHandlerThread.quit();
}
}