intent.putExtra("hello","您好")//键值对
接受页面拿到intent
Intent intent = getintent();
String hello = getStringExtra(“hello”);
Textview tv = new Textview;
tv.setText(hello);
Bundle
Bundle主要用于传递数据,它保存的数据,是以key-value(键值对)的形式存在的。
Bundle经常使用在Activity之间或者线程之间传递数据,传递的数据可以是boolean,byte,int ,long,float,double,string等基本数据类型或它们对应的数组,也可以是对象或对象数组。
当Bundle传递的是对象或对象数组时,必须实现Serializable或Parcelable接口。
Bundle提供了各种常用类型的putXxx()/getXxx方法,用于读写基本类型的数据。
在activity间传递信息
Bundle bundle = new Bundle(); //得到bundle对象
bundle.putString("sff","value"值); //key-"sff",通过得到value-"value"值(String型)
bundle.putInt("iff","175"); //key-"iff",value-175
intent.putintExtra(bundle); //通过intent将bundle传递给另一个Activity
startActivity(intent);
读取数据
Bundle bundle = this.getIntent().getExtra(); //读取intent的数据给bundle对象
String str1 = bundle.getString("sff"); // 通过key得到value
int int1 = bundle.getInt("iff");
线程间传递
通过Handle将带有bundle数据的message放入消息队列,其他线程就可以从队列中得到数据。
Message message = new Message(); //new一个Message对象
message.what = MESSAGE_WHAT_2; //给消息做标记
Bundle bundle = new Bundle(); //得到Bundle对象
bundle.putString("text1","消息传递参数的列子!") // 往Bundle中存放数据
bundle.putInt("text2",44); //往Bundle中put数据
message.setData(bundle); //mes利用Bundle传递数据
mHandler.sendMessage(message); //Handle将消息放入消息队列
读取数据
这里用的Handler的handleMessage(Message msg);方法处理数据
String str1 = msg.getData().getString("text1");
Int int = msg.getData().getString("text2");