intent传递基本数据还有对象和集合的总结
一. intent 来传递数据
(系统提供的方便方法)
通过intent.putExtra把常用的基本类型给放置进来
intent.putExtra(name, boolean);
intent.putExtra(name, long);
intent.putExtra(name, short);
intent.putExtra(name, int);
取数据
intent.getIntExtra(name, defaultValue);
intent.getBooleanExtra(name, defaultValue);
在别的地方获取intent的方法
首先要得到context对象.context.getIntent();然后调用getXXXExtra(String KEY);
二:android给我们提供了一个方便的对象 Bundle 他类似于map
Bundle bundle = new Bundle();
bundle.putBoolean(key, value);
bundle.putByte(key, value);
bundle.putCharArray(key, value);
Intent intent = new Intent();
intent.putExtras(bundle);
取数据
Bundle bundle = intent.getExtras();
bundle.getBoolean(String Key);
bundle.getint(String Key);
....
一、二两种方法本质是一样的。因为Intent本身带有一个空的Bundle对象;Bundle存储数据的实现原理是Map的方式
如何传递复杂的数据类型(Object List<object>),所传递的对象必须实现Serializable或者Parcelable这两个接口
传递的对象被改变,不会改变原来的值
案例一:传递和接收实现Serializable接口的对象
1) User.java(implements Serializable)
2) MainActivity.java
User user = new User;
Intent intent = new Intent(this,Second.class);
intent.putExtra("User",user);
3) Second.java
Intent intent = getIntent( );
User user = intent.getSerializableExtra("User" );
2、实现Parcelable接口
1)为什么要实现Parfcelable接口来实现在Intent中传递对象?
a、在使用内存的时候,Parcelable比Serializable性能高,所以推荐使用Parcelable类。
b、Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
注意:Parcelable不能使用在将数据存储在磁盘上的情况,因为Parcelable不能很好的保存数据的持续性在外界有变化的情况下。因此在这种情况下,建议使用Serializable
2) Android中的新的序列化机制
在Android系统中,针对内存受限的移动设备,因此对性能要求更高,Android系统采用了新的IPC(进程间通信)机制,要求使用性能更出色的对象传输方式。因此Parcel类被设计出来,其定位就是轻量级的高效的对象序列化和反序列化机制。
Parcel的序列化和反序列化的读写全是在内存中进行,所以效率比JAVA序列化中使用外部存储器会高很多。
Parcel类
就应用程序而言,在常使用Parcel类的场景就是在Activity间传递数据。在Activity间使用Intent传递数据的时候,可以通过Parcelable机制传递复杂的对象。
Parcel机制:本质上把它当成一个Serialize就可以了。只是Parcel的对象实在内存中完成的序列化和反序列化,利用的是连续的内存空间,因此更加高效。
案例:
步骤1:自定义实体类,实现Parcelable接口,重写其两个方法。
步骤2:该实体类必须添加一个常量CREATOR(名字大小写都不能使其他的),该常量必须实现Parcelable的内部接口:Parcelable.Creator,并实现该接口中的两个方法。
User.java如下:
- package com.example.intent_object;
- import android.os.Parcel;
- import android.os.Parcelable;
- public class User implements Parcelable {
- public String name;
- public int age;
- // 必须要创建一个名叫CREATOR的常量。
- public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
- @Override
- public User createFromParcel(Parcel source) {
- return new User(source);
- }
- //重写createFromParcel方法,创建并返回一个获得了数据的user对象
- @Override
- public User[] newArray(int size) {
- return new User[size];
- }
- };
- @Override
- public String toString() {
- return name + ":" + age;
- }
- // 无参数构造器方法,供外界创建类的实例时调用
- public User() {
- }
- // 带参构造器方法私用化,本构造器仅供类的方法createFromParcel调用
- private User(Parcel source) {
- name = source.readString();
- age = source.readInt();
- }
- @Override
- public int describeContents() {
- return 0;
- }
- // 将对象中的属性保存至目标对象dest中
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- dest.writeString(name);
- dest.writeInt(age);
- }
- //省略getter/setter }
其他代码:
- Bundle bundle = new Bundle();
- bundle.putParcelable("user", user);
- Intent intent = new Intent(MainActivity.this,
- SecondActivity.class);
- intent.putExtras(bundle);
- Intent intent = getIntent();
- Bundle bun = intent.getExtras();
- User user = bun.getParcelable("user");
- System.out.println(user);
三在线程中使用Message传递数据时的方法
message.arg1 = 放入一个整形数据
message.what = 整形对象
message.obj = 放一个对象;
message.setData = 放入一个Bundle对象

浙公网安备 33010602011771号