Android Intent是在Android应用程序之间提供运行时绑定的一种重要机制。它可以在应用程序的不同组件之间传输数据,例如在Activity,Service,BroadcastReceiver和ContentProvider之间。
一、传递基本数据类型
在Android Intent中,我们可以使用putExtra方法来传递基本数据类型,例如字符串,整数和布尔值。这里有一个示例代码:
Intent i = new Intent(this, OtherActivity.class);
i.putExtra("KEY_STRING", "Hello, World!");
i.putExtra("KEY_INT", 12345);
i.putExtra("KEY_BOOLEAN", true);
startActivity(i);
在接收方Activity中,我们可以使用getExtra方法获得传递的数据:
Intent i = getIntent();
String str = i.getStringExtra("KEY_STRING");
int num = i.getIntExtra("KEY_INT");
boolean bool = i.getBooleanExtra("KEY_BOOLEAN");
二、传递序列化对象
在Android Intent中,我们可以使用Serializable接口来传递自定义对象。Serializable接口是Java中的一个标记接口,通过实现它,我们可以实现让对象将自身的状态信息序列化为一组字节来进行传输和保存。
这里有一个自定义对象Person示例:
public class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
在传递Person对象的Activity中,我们可以这样写:
Intent i = new Intent(this, OtherActivity.class);
Person person = new Person("Bob", 25);
i.putExtra("KEY_PERSON", person);
startActivity(i);
在接收方Activity中,我们可以这样写来获得Person对象:
Intent i = getIntent();
Person person = (Person) i.getSerializableExtra("KEY_PERSON");
String name = person.getName();
int age = person.getAge();
三、传递Parcelable对象
使用Parcelable接口也可以通过Android Intent传递自定义对象。Parcelable接口是一个比Serializable更高效的方式,因为它们不需要通过反射来实现序列化和反序列化,而是直接将对象转换为字节数组进行传输。
这里有一个自定义对象Person示例:
public class Person implements Parcelable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
protected Person(Parcel in) {
name = in.readString();
age = in.readInt();
}
public static final Creator CREATOR = new Creator() {
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
}
在传递Person对象的Activity中,我们可以这样写:
Intent i = new Intent(this, OtherActivity.class);
Person person = new Person("Bob", 25);
i.putExtra("KEY_PERSON", person);
startActivity(i);
在接收方Activity中,我们可以这样写来获得Person对象:
Intent i = getIntent();
Person person = i.getParcelableExtra("KEY_PERSON");
String name = person.getName();
int age = person.getAge();
四、传递集合
在Android Intent中,我们可以使用Bundle对象来传递集合。Bundle是一种键值对的数据结构,它可以在Activity之间传递复杂数据结构。
这里有一个ArrayList集合的代码示例:
Intent i = new Intent(this, OtherActivity.class);
ArrayList list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
Bundle bundle = new Bundle();
bundle.putStringArrayList("KEY_LIST", list);
i.putExtras(bundle);
startActivity(i);
在接收方Activity中,我们可以这样写来获得集合:
Intent i = getIntent();
Bundle bundle = i.getExtras();
ArrayList list = bundle.getStringArrayList("KEY_LIST");
for (String str : list) {
Log.d(TAG, "Item: " + str);
}
五、传递Parcelable集合
使用Parcelable接口也可以通过Android Intent传递集合。
这里有一个ArrayList集合的代码示例:
Intent i = new Intent(this, OtherActivity.class);
ArrayList list = new ArrayList();
list.add(new Person("Bob", 25));
list.add(new Person("Mary", 30));
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("KEY_LIST", list);
i.putExtras(bundle);
startActivity(i);
在接收方Activity中,我们可以这样写来获得集合:
Intent i = getIntent();
Bundle bundle = i.getExtras();
ArrayList list = bundle.getParcelableArrayList("KEY_LIST");
for (Person person : list) {
String name = person.getName();
int age = person.getAge();
Log.d(TAG, "Name: " + name + ", Age: " + age);
}
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/295266.html
微信扫一扫
支付宝扫一扫