当前位置:编程学习 > JAVA >>

android中有关handler的问题

package com.example.lianxi7;

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println(Thread.currentThread().getId());
HandlerThread handlerthread=new HandlerThread("handler_thread");
handlerthread.start();
MyHandler handler=new MyHandler(handlerthread.getLooper());
Message msg=handler.obtainMessage();
Bundle b=new Bundle();
b.putInt("age", 22);
b.putString("name", "liu");
msg.setData(b);
msg.sendToTarget();

 System.out.println("dhghgdfh");
}
     class MyHandler extends Handler{
      public MyHandler(){
     }
      public MyHandler(Looper looper){
      super(looper);
      }
      
      public void handlerMessage(Message msg){
      Bundle b=msg.getData();
      int age=b.getInt("age");
      System.out.println(age);
     System.out.println("gdgdf");
      System.out.println(Thread.currentThread().getId());
      
      }
    }
@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;
}

}
运行的时候代码public void handlerMessage(Message msg){
      Bundle b=msg.getData();
      int age=b.getInt("age");
      System.out.println(age);
     System.out.println("gdgdf");
      System.out.println(Thread.currentThread().getId());
      中的3个System.out语句没有输出 怎么回事啊  求解啊 Android 线程 handler
补充:Java ,  Eclipse
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,