Android Bundle的使用

时间:2023-03-09 23:05:25
Android Bundle的使用
发送数据:

Bundle bundle = new Bundle();

bundle.putString("sex" , "男人");
bundle.putDouble("height" , 40,38);
Intent intent = new Intent();
intent.putExtras(bundle);
intent.setClass(ThisOne.this , ThatOne.class);
startActivity(intent);
接收数据:
Bundle bundle = this.getIntent().getExtras();
String sex = bundle.getString("sex");
double height = bundle.getDouble("height");
====================================
在Message中使用Bundle传递数据
Message msg = myHandler.obtainMessage();
Bundle b = new Bundle();
b.putInt("age" , 20);
b.putString("name" , "Jon");
msg.setData(b);
msg.sendToTarget();
class MyHandler extends Handler{
   public void handleMessage(Message msg){
      Bundle b = msg.getData();
      int age = b.getInt("age");
      String name = b.getString("name");
  }
}