现在的位置: 首页 > 综合 > 正文

android的handler异步线程

2018年01月16日 ⁄ 综合 ⁄ 共 711字 ⁄ 字号 评论关闭

由于家里机器上没装eclipse,下为手写,可能有格式错误:

public class MainActivity extends Activity {

     TextView tv = (TextView) findViewById(R.id.tv);

     HandlerThread ht = new Handlerthread("wench");

     ht.start();//启动新线程

 

     MyHandler mh = new MyHandler(ht.getLooper());

     Message msg = mh.obtainMessage();

     Bundle bundle = new Bundle();

     bundle.putString("name","xiaoming");

      bundle.putInt("age",26);

      msg.setData(bundle);

      msg.sendTotarget();//进入消息队列

 

     class MyHandler extends Handler {

         public MyHandler(Looper looper){

            super(looper);

        }

      public void handleMessage(Message msg){

          Bundle b = msg.getData();

          String name = b.getString("name");

           int age = b.getInt("age");

          tv.setText(name);

      }

    }

 

 

}

【上篇】
【下篇】

抱歉!评论已关闭.