Android Activity - Intent 数据传递(基础数据类型、字符串与字符序列、数组类型、集合类型、Parcelable 对象、Serializable 对象)

一、基础数据类型

1、基本介绍
  • 支持的类型:boolean、byte、char、short、int、long、float、double
2、演示
  1. 发送端
Intentintent=newIntent(this,ReceiveDataActivity.class);intent.putExtra("is_active",true);intent.putExtra("byte_data",(byte)10);intent.putExtra("initial",'A');intent.putExtra("short_num",(short)100);intent.putExtra("age",25);intent.putExtra("timestamp",System.currentTimeMillis());intent.putExtra("price",19.99f);intent.putExtra("pi",3.1415926535);startActivity(intent);
  1. 接收端
Intentintent=getIntent();booleanisActive=intent.getBooleanExtra("is_active",false);bytebyteData=intent.getByteExtra("byte_data",(byte)0);charinitial=intent.getCharExtra("initial",'\0');shortshortNum=intent.getShortExtra("short_num",(short)0);intage=intent.getIntExtra("age",0);longtimestamp=intent.getLongExtra("timestamp",0);floatprice=intent.getFloatExtra("price",0.0f);doublepi=intent.getDoubleExtra("pi",0.0);Log.i(TAG,"isActive: "+isActive);Log.i(TAG,"byteData: "+byteData);Log.i(TAG,"initial: "+initial);Log.i(TAG,"shortNum: "+shortNum);Log.i(TAG,"age: "+age);Log.i(TAG,"timestamp: "+timestamp);Log.i(TAG,"price: "+price);Log.i(TAG,"pi: "+pi);
# 输出结果 isActive: true byteData: 10 initial: A shortNum: 100 age: 25 timestamp: 1752133742344 price: 19.99 pi: 3.1415926535

二、字符串与字符序列

1、基本介绍
  • 支持的类型:String、CharSequence
2、演示
  1. 发送端
Intentintent=newIntent(this,ReceiveDataActivity.class);intent.putExtra("username","Alice");intent.putExtra("message",(CharSequence)"Hello");startActivity(intent);
  1. 接收端
Intentintent=getIntent();Stringusername=intent.getStringExtra("username");CharSequencemessage=intent.getCharSequenceExtra("message");Log.i(TAG,"username: "+username);Log.i(TAG,"message: "+message);
# 输出结果 username: Alice message: Hello

三、数组类型

1、基本介绍
  1. 支持的基本类型数组:boolean[]byte[]char[]short[]int[]long[]float[]double[]

  2. 支持的对象类型数组:String[]CharSequence[]

2、演示
  1. 发送端
Intentintent=newIntent(this,ReceiveDataActivity.class);intent.putExtra("flags",newboolean[]{true,false});intent.putExtra("byte_array",newbyte[]{1,2,3});intent.putExtra("short_array",newshort[]{10,20});intent.putExtra("chars",newchar[]{'A','B'});intent.putExtra("numbers",newint[]{1,2,3});intent.putExtra("long_array",newlong[]{100L,200L});intent.putExtra("float_array",newfloat[]{1.1f,2.2f});intent.putExtra("double_array",newdouble[]{1.1,2.2});intent.putExtra("names",newString[]{"Alice","Bob"});intent.putExtra("texts",newCharSequence[]{"Hi","Hello"});startActivity(intent);
  1. 接收端
Intentintent=getIntent();boolean[]flags=intent.getBooleanArrayExtra("flags");byte[]byteArray=intent.getByteArrayExtra("byte_array");short[]shortArray=intent.getShortArrayExtra("short_array");char[]chars=intent.getCharArrayExtra("chars");int[]numbers=intent.getIntArrayExtra("numbers");long[]longArray=intent.getLongArrayExtra("long_array");float[]floatArray=intent.getFloatArrayExtra("float_array");double[]doubleArray=intent.getDoubleArrayExtra("double_array");String[]names=intent.getStringArrayExtra("names");CharSequence[]texts=intent.getCharSequenceArrayExtra("texts");Log.i(TAG,"flags: "+Arrays.toString(flags));Log.i(TAG,"byteArray: "+Arrays.toString(byteArray));Log.i(TAG,"shortArray: "+Arrays.toString(shortArray));Log.i(TAG,"chars: "+Arrays.toString(chars));Log.i(TAG,"numbers: "+Arrays.toString(numbers));Log.i(TAG,"longArray: "+Arrays.toString(longArray));Log.i(TAG,"floatArray: "+Arrays.toString(floatArray));Log.i(TAG,"doubleArray: "+Arrays.toString(doubleArray));Log.i(TAG,"names: "+Arrays.toString(names));Log.i(TAG,"texts: "+Arrays.toString(texts));
# 输出结果 flags: [true, false] byteArray: [1, 2, 3] shortArray: [10, 20] chars: [A, B] numbers: [1, 2, 3] longArray: [100, 200] floatArray: [1.1, 2.2] doubleArray: [1.1, 2.2] names: [Alice, Bob] texts: [Hi, Hello]

四、集合类型

1、基本介绍
  • 支持的类型:ArrayList<Integer>ArrayList<String>ArrayList<CharSequence>
2、演示
  1. 发送端
ArrayList<Integer>numbersList=newArrayList<>();numbersList.add(100);numbersList.add(200);numbersList.add(300);ArrayList<String>namesList=newArrayList<>();namesList.add("张三");namesList.add("李四");namesList.add("王五");ArrayList<CharSequence>textsList=newArrayList<>();textsList.add("第一行");textsList.add("第二行");textsList.add("第三行");Intentintent=newIntent(this,ReceiveDataActivity.class);intent.putIntegerArrayListExtra("numbers_list",numbersList);intent.putStringArrayListExtra("names_list",namesList);intent.putCharSequenceArrayListExtra("texts_list",textsList);startActivity(intent);
  1. 接收端
Intentintent=getIntent();ArrayList<Integer>numbersList=intent.getIntegerArrayListExtra("numbers_list");ArrayList<String>namesList=intent.getStringArrayListExtra("names_list");ArrayList<CharSequence>textsList=intent.getCharSequenceArrayListExtra("texts_list");Log.i(TAG,"numbersList: "+numbersList);Log.i(TAG,"namesList: "+namesList);Log.i(TAG,"textsList: "+textsList);
# 输出结果 numbersList: [100, 200, 300] namesList: [张三, 李四, 王五] textsList: [第一行, 第二行, 第三行]

五、Parcelable 对象

1、基本介绍
  • 定义类实现 Parcelable 接口,实现writeToParcel()describeContents()和 CREATOR
2、演示
publicclassUserimplementsParcelable{privateStringname;privateintage;publicUser(Stringname,intage){this.name=name;this.age=age;}protectedUser(Parcelin){name=in.readString();age=in.readInt();}@OverridepublicintdescribeContents(){return0;}@OverridepublicvoidwriteToParcel(@NonNullParcelparcel,inti){parcel.writeString(name);parcel.writeInt(age);}publicstaticfinalCreator<User>CREATOR=newCreator<User>(){@OverridepublicUsercreateFromParcel(Parcelparcel){returnnewUser(parcel);}@OverridepublicUser[]newArray(inti){returnnewUser[i];}};@OverridepublicStringtoString(){return"User{"+"name='"+name+'\''+", age="+age+'}';}}
  1. 发送端
Useruser=newUser("张三",25);User[]users=newUser[]{newUser("李四",30),newUser("王五",28)};ArrayList<User>userList=newArrayList<>();userList.add(newUser("赵六",22));userList.add(newUser("钱七",35));Intentintent=newIntent(this,ReceiveDataActivity.class);intent.putExtra("single_user",user);intent.putExtra("user_array",users);intent.putParcelableArrayListExtra("user_list",userList);startActivity(intent);
  1. 接收端
Intentintent=getIntent();UsersingleUser=intent.getParcelableExtra("single_user");Parcelable[]userArray=intent.getParcelableArrayExtra("user_array");ArrayList<User>userList=intent.getParcelableArrayListExtra("user_list");Log.i(TAG,"singleUser: "+singleUser);Log.i(TAG,"userArray: "+Arrays.toString(userArray));Log.i(TAG,"userList: "+userList);
# 输出结果 singleUser: User{name='张三', age=25} userArray: [User{name='李四', age=30}, User{name='王五', age=28}] userList: [User{name='赵六', age=22}, User{name='钱七', age=35}]

六、Serializable 对象

1、基本介绍
  • 定义类实现 Serializable 接口,简单易用但性能较差
2、演示
  1. 发送端
Staffstaff=newStaff("张三",25);Intentintent=newIntent(this,ReceiveDataActivity.class);intent.putExtra("staff",staff);startActivity(intent);
  1. 接收端
Intentintent=getIntent();Staffstaff=(Staff)intent.getSerializableExtra("staff");Log.i(TAG,"staff: "+staff);
# 输出结果 staff: Staff{name='张三', age=25}