Intent 是Android 程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前组
件想要执行的动作,还可以在不同组件之间传递数据。Intent 一般可被用于启动活动、启动
服务、以及发送广播等场景
// A activity调用 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); // 创建视图 setContentView(R.layout.my_layout); // 找到对应的button来监听事件 findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, AnotherAty.class); i.putExtra("data", "hello word"); // 使用Intent来传参 startActivity(i); } }); System.out.println("onCreate"); }
//B activity 通过Intent来获取值,并显示在textView上面 private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_another_aty); Intent i = getIntent(); //直接获取传过来的intent tv = (TextView)findViewById(R.id.textView); tv.setText(i.getStringExtra("data")); }
// 如果数据比较多,可以通过 Bundle 数据包来传递数据
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); // 创建视图 setContentView(R.layout.my_layout); // 找到对应的button来监听事件 findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, AnotherAty.class); //i.putExtra("data", "hello word"); // 使用Intent来传参 Bundle b = new Bundle(); // 打包数据 b.putString("name", "chengzhier"); b.putInt("age", 2); i.putExtras(b); startActivity(i); } }); System.out.println("onCreate"); } private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_another_aty); Intent i = getIntent(); //直接获取传过来的intent tv = (TextView)findViewById(R.id.textView); //i.getStringExtra("data") Bundle data = i.getExtras(); String s = String.format("name=%s, age=%d", data.getString("name"), data.getInt("age")); tv.setText(s); }
// 传递一个对象 java 自带的 Serializable 虚拟化
// User类 public class User implements Serializable{ //让这个对象序列化 private String name; private int age; public User(int age, String name ) { this.age = age; this.name = name; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public String getName() { return name; } } // A activity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); // 创建视图 setContentView(R.layout.my_layout); // 找到对应的button来监听事件 findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, AnotherAty.class); i.putExtra("user", new User(2, "zh")); startActivity(i); } }); System.out.println("onCreate"); } // B activiry private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_another_aty); Intent i = getIntent(); //直接获取传过来的intent tv = (TextView)findViewById(R.id.textView); User u = (User)i.getSerializableExtra("user"); //取出来 String s = String.format("测试11name=%s, age=%d", u.getName(), u.getAge()); tv.setText(s); }
//传递一个对象 安卓专门的Parcelable虚拟化
/** * Created by ZhouXiaoHai on 2016/9/8. User 类 */ public class User implements Parcelable{ // 安卓自带的序列化 private int age; private String name; private String dogName; public void setDogName(String dogName) { this.dogName = dogName; } public String getDogName() { return dogName; } public User(int age, String name, String dogName) { this.age = age; this.name = name; this.dogName = dogName; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public String getName() { return name; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { // 必须要写的 接口 Parcelable 的方法 // 这个一定要按变量顺序写 dest.writeInt(getAge()); dest.writeString(getName()); dest.writeString(getDogName()); } public static final Creator<User> CREATOR = new Creator<User>() { @Override public User createFromParcel(Parcel source) { // 这个一定要按变量顺序写 return new User( source.readInt(), source.readString(), source.readString()); } @Override public User[] newArray(int size) { return new User[size]; } }; } //A activity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); // 创建视图 setContentView(R.layout.my_layout); // 找到对应的button来监听事件 findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, AnotherAty.class); i.putExtra("user", new User(2, "zh", "旺财")); startActivity(i); } }); System.out.println("onCreate"); } //B activity private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_another_aty); Intent i = getIntent(); //直接获取传过来的intent tv = (TextView)findViewById(R.id.textView); //User u = (User)i.getSerializableExtra("user"); User u = (User)i.getParcelableExtra("user"); String s = String.format("测试21name=%s, age=%d 狗的名字=%s", u.getName(), u.getAge(), u.getDogName()); tv.setText(s); }
简单总结(小白): Serializable 比 Parcelable 使用起来方便,直接实现接口就好了,但是效率不高。 Parcelable效率高,但是需要自己写一些代码。