标题:关于handlerThread中run、start的问题
只看楼主
我是胖子i
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-11-5
 问题点数:0 回复次数:1 
关于handlerThread中run、start的问题
问题:我调用了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 编辑 ]
搜索更多相关主题的帖子: package public import start 
2013-11-05 22:14
我是胖子i
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-11-5
得分:0 
后续问题出来了,我把代码里面的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}

为啥呢?

2013-11-05 22:58



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-423109-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015703 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved