- public class Intent implements Parcelable, Cloneable {
- private Uri mData;
- private String mType;
- private String mPackage;
- private ComponentName mComponent;
- private int mFlags;
- private HashSet<String> mCategories;
- private Bundle mExtras;
- private Rect mSourceBounds;
Intent也是继承了Parcelable的接口。
个人理解,Intent应该只是一个数据参数的载体,真正将两个Acitivity/Service通信起来的是Binder接口(C/S架构)。
第一类:简单或基本数据类型
- Intent putExtra(String name, int[] value)
- Intent putExtra(String name, float value)
- Intent putExtra(String name, byte[] value)
- Intent putExtra(String name, long[] value)
- Intent putExtra(String name, float[] value)
- Intent putExtra(String name, long value)
- Intent putExtra(String name, String[] value)
- Intent putExtra(String name, boolean value)
- Intent putExtra(String name, boolean[] value)
- Intent putExtra(String name, short value)
- Intent putExtra(String name, double value)
- Intent putExtra(String name, short[] value)
- Intent putExtra(String name, String value)
- Intent putExtra(String name, byte value)
- Intent putExtra(String name, char[] value)
- Intent putExtra(String name, CharSequence[] value)
本质上仍然是通过一个Bundle(private Bundle mExtras;)来实现:
- public Intent putExtra(String name, long value) {
- if (mExtras == null) {
- mExtras = new Bundle();
- }
- mExtras.putLong(name, value);
- return this;
- }
第二类:传递一个Bundle
- public Intent putExtra(String name, Bundle value) {
- if (mExtras == null) {
- mExtras = new Bundle();
- }
- mExtras.putBundle(name, value);
- return this;
- }
第三类:传递Serializable对象
- public Intent putExtra(String name, Serializable value) {
- if (mExtras == null) {
- mExtras = new Bundle();
- }
- mExtras.putSerializable(name, value);
- return this;
- }
第四类:Parcelable对象
- public Intent putExtra(String name, Parcelable value) {
- if (mExtras == null) {
- mExtras = new Bundle();
- }
- mExtras.putParcelable(name, value);
- return this;
- }
- public Intent putExtra(String name, Parcelable[] value) {
- if (mExtras == null) {
- mExtras = new Bundle();
- }
- mExtras.putParcelableArray(name, value);
- return this;
- }
第五类:Intent
- public Intent putExtras(Intent src) {
- if (src.mExtras != null) {
- if (mExtras == null) {
- mExtras = new Bundle(src.mExtras);
- } else {
- mExtras.putAll(src.mExtras);
- }
- }
- return this;
- }
归根结底都是通过Bundle来实现数据封装。而Bundle则是通过Map的数据结构来存储数据。
mMap = new HashMap<String, Object>();
mParcelledData
两者同时只有一个有效。
一旦unparcel以后,mParcelledData
的数据将被填充到mMap中,同时值为null。在writeToParcel和readFromParcel中则直接使用mParcelledData.此时一般通过IBinder关联两个进程的通信。
关于Bundle则是实现了Parcelable接口的类,通过上面提到的HashMap和一个Parcel来存储数据。
- public final class Bundle implements Parcelable, Cloneable {
- private static final String LOG_TAG = "Bundle";
- public static final Bundle EMPTY;
-
- static {
- EMPTY = new Bundle();
- EMPTY.mMap = Collections.unmodifiableMap(new HashMap<String, Object>());
- }
-
-
-
-
- Map<String, Object> mMap = null;
-
-
-
-
-
-
- Parcel mParcelledData = null;
- public void putFloat(String key, float value) {
- unparcel();
- mMap.put(key, value);
- }
-
-
- synchronized void unparcel() {
- if (mParcelledData == null) {
- return;
- }
-
- int N = mParcelledData.readInt();
- if (N < 0) {
- return;
- }
- if (mMap == null) {
- mMap = new HashMap<String, Object>();
- }
- mParcelledData.readMapInternal(mMap, N, mClassLoader);
- mParcelledData.recycle();
- mParcelledData = null;
- }