注册 登录
编程论坛 Android开发

关于handlerThread中run、start的问题

我是胖子i 发布于 2013-11-05 22:14, 3604 次点击
问题:我调用了handlerthread.start()
为啥没有执行上面的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;
    }
}




[ 本帖最后由 我是胖子i 于 2013-11-5 22:17 编辑 ]
1 回复
#2
我是胖子i2013-11-05 22:58
后续问题出来了,我把代码里面的super.run注释掉了结果run里面执行了,但是还是有点问题,程序不继续打印了,在我理解应该是打印
This Thread is not run
This Thread is run
this is a joke!


但是结果
11-05 14:50:19.253: INFO/System.out(612): main
11-05 14:50:19.263: INFO/System.out(612): This Thread is run
11-05 14:50:19.263: INFO/System.out(612): MyThread
11-05 14:50:28.748: WARN/ActivityManager(60): Launch timeout has expired, giving up wake lock!
11-05 14:50:29.040: WARN/ActivityManager(60): Activity idle timeout for HistoryRecord{405b9ab0 yu.looper/.MainActivity}

为啥呢?

1