Android多线程--HandlerThread用法

 

package com.proper;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;

public class HandlerThreadActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        System.out.println("线程Id"+Thread.currentThread().getId());
        HandlerThread handlerThread=new HandlerThread("handlerThread");
        handlerThread.start();
        MyHandler myHandler=new MyHandler(handlerThread.getLooper());
        Message msg=myHandler.obtainMessage();
        Bundle bundle=new Bundle();
        bundle.putInt("age", 24);
        bundle.putString("name", "Robin");
        msg.setData(bundle);
        msg.sendToTarget();
    }
    class MyHandler extends Handler{

     public MyHandler(){}
     public MyHandler(Looper looper ){
      super(looper);
     }
  @Override
  public void handleMessage(Message msg) {
   // TODO Auto-generated method stub
   super.handleMessage(msg);
   Bundle bundle=msg.getData();
   int age=bundle.getInt("age");
      String name=bundle.getString("name");
   System.out.println("线程Id"+Thread.currentThread().getId());
   System.out.println("名字"+name+"-------"+"年龄"+age);
   
  }
     
    }
}

你可能感兴趣的:(多线程,android,String,Class)