public class LooperThread extends Thread{
public Handler handler1;
@Override
public void run(){
super.run();
Looper.prepare();
handler1=new Handler(){
public void handleMessage(Message msg){
Log.i("Looper",String.valueOf(msg.what));
}
};
Message m=handler1.obtainMessage();
m.what=0x11;
handler1.sendMessage(m);
Looper.loop();
}
}
然后,在MainActivity中的onCreate方法中,创建一个LooperThread线程,并开启该线程,关键代码如下:
LooperThread thread=new LooperThread();
thread.start();