1. 利用Intent对象携带简单数据
利用Intent的Extra部分来存储我们想要传递的数据,可以传送int, long, char等一些基础类型,对复杂的对象就无能为力了。
1.1 设置参数
[java] view plain copy
1. //传递些简单的参数
2. Intent intentSimple = new Intent();
3. intentSimple.setClass(MainActivity.this,SimpleActivity.class);
4.
5. Bundle bundleSimple = new Bundle();
6. bundleSimple.putString("usr", "xcl");
7. bundleSimple.putString("pwd", "zj");
8. intentSimple.putExtras(bundleSimple);
9.
10. startActivity(intentSimple);
1.2 接收参数
[java] view plain copy
1. this.setTitle("简单的参数传递例子");
2.
3. //接收参数
4. Bundle bunde = this.getIntent().getExtras();
5. String eml = bunde.getString("usr");
6. String pwd = bunde.getString("pwd");
2. 利用Intent对象携带如ArrayList之类复杂些的数据
这种原理是和上面一种是一样的,只是要注意下。 在传参数前,要用新增加一个List将对象包起来。
2.1 设置参数
[java] view plain copy
1. //传递复杂些的参数
2. Map<String, Object> map1 = new HashMap<String, Object>();
3. map1.put("key1", "value1");
4. map1.put("key2", "value2");
5. List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
6. list.add(map1);
7.
8. Intent intent = new Intent();
9. intent.setClass(MainActivity.this,ComplexActivity.class);
10. Bundle bundle = new Bundle();
11. //须定义一个list用于在budnle中传递需要传递的ArrayList<Object>,这个是必须要的
12. ArrayList bundlelist = new ArrayList();
13. bundlelist.add(list);
14. bundle.putParcelableArrayList("list",bundlelist);
15. intent.putExtras(bundle);
16. startActivity(intent);
2.1 接收参数
[java] view plain copy
1. <span style="white-space:pre"> </span> this.setTitle("复杂参数传递例子");
2.
3. //接收参数
4. Bundle bundle = getIntent().getExtras();
5. "list");
6. //从List中将参数转回 List<Map<String, Object>>
7. 0);
8.
9. "";
10. for (Map<String, Object> m : lists)
11. {
12. for (String k : m.keySet())
13. {
14. "\r\n"+k + " : " + m.get(k);
15. }
16. }
3. 通过实现Serializable接口
3.1 设置参数
利用Java语言本身的特性,通过将数据序列化后,再将其传递出去。
[java] view plain copy
1. //通过Serializable接口传参数的例子
2. HashMap<String,String> map2 = new HashMap<String,String>();
3. map2.put("key1", "value1");
4. map2.put("key2", "value2");
5.
6. Bundle bundleSerializable = new Bundle();
7. bundleSerializable.putSerializable("serializable", map2);
8. Intent intentSerializable = new Intent();
9. intentSerializable.putExtras(bundleSerializable);
10. intentSerializable.setClass(MainActivity.this,
11. class);
12. startActivity(intentSerializable);
3.2 接收参数
[java] view plain copy
1. this.setTitle("Serializable例子");
2.
3. //接收参数
4. this.getIntent().getExtras();
5. //如果传 LinkedHashMap,则bundle.getSerializable转换时会报ClassCastException,不知道什么原因
6. //传HashMap倒没有问题。
7. "serializable");
8.
9. String sResult = "map.size() ="+map.size();
10.
11. Iterator iter = map.entrySet().iterator();
12. while(iter.hasNext())
13. {
14. Map.Entry entry = (Map.Entry)iter.next();
15. Object key = entry.getKey();
16. Object value = entry.getValue();
17. "\r\n key----> "+(String)key;
18. "\r\n value----> "+(String)value;
19. }
4. 通过实现Parcelable接口
这个是通过实现Parcelable接口,把要传的数据打包在里面,然后在接收端自己分解出来。这个是Android独有的,在其本身的源码中也用得很多,
效率要比Serializable相对要好。
4.1 首先要定义一个类,用于 实现Parcelable接口
因为其本质也是序列化数据,所以这里要注意定义顺序要与解析顺序要一致噢。
[java] view plain copy
1. public class XclParcelable implements Parcelable {
2.
3. //定义要被传输的数据
4. public int mInt;
5. public String mStr;
6. public HashMap<String,String> mMap = new HashMap<String,String>();
7.
8. //Describe the kinds of special objects contained in this Parcelable's marshalled representation.
9. public int describeContents() {
10. return 0;
11. }
12.
13. //Flatten this object in to a Parcel.
14. public void writeToParcel(Parcel out, int flags) {
15. //等于将数据映射到Parcel中去
16. out.writeInt(mInt);
17. out.writeString(mStr);
18. out.writeMap(mMap);
19. }
20.
21. //Interface that must be implemented and provided as a public CREATOR field
22. //that generates instances of your Parcelable class from a Parcel.
23. public static final Parcelable.Creator<XclParcelable> CREATOR
24. new Parcelable.Creator<XclParcelable>() {
25. public XclParcelable createFromParcel(Parcel in) {
26. return new XclParcelable(in);
27. }
28.
29. public XclParcelable[] newArray(int size) {
30. return new XclParcelable[size];
31. }
32. };
33.
34. private XclParcelable(Parcel in) {
35. //将映射在Parcel对象中的数据还原回来
36. //警告,这里顺序一定要和writeToParcel中定义的顺序一致才行!!!
37. mInt = in.readInt();
38. mStr = in.readString();
39. class.getClassLoader());
40. }
41.
42. public XclParcelable() {
43. // TODO Auto-generated constructor stub
44. }
45. }
4.2 设置参数
[java] view plain copy
1. //通过实现Parcelable接口传参的例子
2. new Intent();
3. new XclParcelable();
4. 1;
5. "字符串";
6. new HashMap<String,String>();
7. "key", "value");
8. "Parcelable", xp);
9. this,
10. class);
11. startActivity(intentParcelable);
4.3 接收参数
[java] view plain copy
1. <span style="white-space:pre"> </span>this.setTitle("Parcelable例子");
2.
3. //接收参数
4. Intent i = getIntent();
5. "Parcelable");
6.
7. TextView tv = (TextView)findViewById(R.id.tv);
8. " mInt ="+xp.mInt
9. "\r\n mStr"+xp.mStr
10. "\r\n size()="+xp.mMap.size());
5. 通过单例模式实现参数传递
单例模式的特点就是可以保证系统中一个类有且只有一个实例。这样很容易就能实现,
在A中设置参数,在B中直接访问了。这是几种方法中效率最高的。
5.1 定义一个单实例的类
[java] view plain copy
1. //单例模式
2. public class XclSingleton
3. {
4. //单例模式实例
5. private static XclSingleton instance = null;
6.
7. //synchronized 用于线程安全,防止多线程同时创建实例
8. public synchronized static XclSingleton getInstance(){
9. if(instance == null){
10. new XclSingleton();
11. }
12. return instance;
13. }
14.
15. final HashMap<String, Object> mMap;
16. private XclSingleton()
17. {
18. new HashMap<String,Object>();
19. }
20.
21. public void put(String key,Object value){
22. mMap.put(key,value);
23. }
24.
25. public Object get(String key)
26. {
27. return mMap.get(key);
28. }
29.
30. }
5.2 设置参数
[java] view plain copy
1. //通过单例模式传参数的例子
2. XclSingleton.getInstance().put("key1", "value1");
3. XclSingleton.getInstance().put("key2", "value2");
4.
5. Intent intentSingleton = new Intent();
6. intentSingleton.setClass(MainActivity.this,
7. class);
8. startActivity(intentSingleton);
5.3 接收参数
[java] view plain copy
1. <span style="white-space:pre"> </span>this.setTitle("单例模式例子");
2. //接收参数
3. HashMap<String,Object> map = XclSingleton.getInstance().mMap;
4. "map.size() ="+map.size();
5.
6. //遍历参数
7. Iterator iter = map.entrySet().iterator();
8. while(iter.hasNext())
9. {
10. Map.Entry entry = (Map.Entry)iter.next();
11. Object key = entry.getKey();
12. Object value = entry.getValue();
13. "\r\n key----> "+(String)key;
14. "\r\n value----> "+(String)value;
android Bundle的使用
bundle的认识:
一种存放字符串和Parcelable类型数据的map类型的容器类,通过存放数据键(key)获取对应的各种类型的值(value),而且必须通过键(key)获取。
bundle的用法:
Bundle相当于Map类,就是一个映射,用Bundle绑定数据,便于数据处理
它主要作用于Activity之间的数据传递.
//TestBundle.java
Bundle bundle = new Bundle();//创建一个句柄
bundle.putString("name", nameinfo);//将nameinfo填充入句柄
Intent mIntent = new Intent(TestBundle.this,TestBundle_getvalue.class);
mIntent.putExtras(bundle);
startActivity(mIntent);
//TestBundle_getvalue.java
Bundle bundle = getIntent().getExtras();//获取一个句柄
String nameString=bundle.get("name");//通过key为“name”来获取value即 nameString.
/*
*进行相关操作
*/
bundle的实例:
第一个Activity发出参数!
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
public class TestBundle extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onTouchEvent(MotionEvent event) {
Intent intent = new Intent();
intent.setClass(TestBundle.this, Target.class);
Bundle mBundle = new Bundle();
mBundle.putString("Data", "hello, bear");//压入数据
intent.putExtras(mBundle);
startActivity(intent);
finish();
return super.onTouchEvent(event);
}
}
第二个Activity,接收参数
import android.app.Activity;
import android.os.Bundle;
public class Target extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle bundle = getIntent().getExtras();
String data=bundle.getString("Data");//读出数据
setTitle(data);
}
}