package newzillion.myhandler2;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
public class MainActivity extends Activity {
private MyThread myThread;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
Log.i("main", "UI.Thread: " + Thread.currentThread());
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
myThread = new MyThread();
myThread.start();
mHandler.sendEmptyMessage(0);
myThread.mHandler.sendEmptyMessage(0);
}
/**
* Create MyThread
*/
class MyThread extends Thread {
private Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
Log.i("main", "Callback MyThread,currentThread: " + Thread.currentThread());
return false;
}
}) {
@Override
public void handleMessage(Message msg) {
Log.i("main", "MyThread.currentThread: " + Thread.currentThread());
}
};
@Override
public void run() {
// Create looper
Looper.prepare();
Log.i("mian", "Thread:" + Thread.currentThread());
Looper.loop();
}
}
}