#2
我是胖子i2013-11-05 22:58
|
为啥没有执行上面的run方法呢??求大神详解 ,小的菜
程序代码:
package yu.looper;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HandlerThread handlerthread = new HandlerThread("MyThread") {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
System.out.println("This Thread is run");
};
};
handlerthread.start();//这里启动了这个线程
System.out.println(Thread.currentThread().getName());
MyHandler myhandle = new MyHandler(handlerthread.getLooper());
Bundle b = new Bundle();
Message msg = myhandle.obtainMessage();
b.putString("abc", "this is a joke!");
msg.setData(b);
msg.sendToTarget();
}
class MyHandler extends Handler {
public MyHandler() {
}
public MyHandler(Looper loop) {
super(loop);
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Bundle b = msg.getData();
String str = b.getString("abc");
System.out.println(str);
}
// public void handlerMessage(Message msg) {
// Bundle b = msg.getData();
// String str = b.getString("abc");
// System.out.println(str);
//
// }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
鸟import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HandlerThread handlerthread = new HandlerThread("MyThread") {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
System.out.println("This Thread is run");
};
};
handlerthread.start();//这里启动了这个线程
System.out.println(Thread.currentThread().getName());
MyHandler myhandle = new MyHandler(handlerthread.getLooper());
Bundle b = new Bundle();
Message msg = myhandle.obtainMessage();
b.putString("abc", "this is a joke!");
msg.setData(b);
msg.sendToTarget();
}
class MyHandler extends Handler {
public MyHandler() {
}
public MyHandler(Looper loop) {
super(loop);
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Bundle b = msg.getData();
String str = b.getString("abc");
System.out.println(str);
}
// public void handlerMessage(Message msg) {
// Bundle b = msg.getData();
// String str = b.getString("abc");
// System.out.println(str);
//
// }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
[ 本帖最后由 我是胖子i 于 2013-11-5 22:17 编辑 ]