- public class Intent implements Parcelable, Cloneable { //... private String mAction;
- 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;
轉自:http://blog.csdn.net/annkie/article/details/8483253
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>());
- }
- // Invariant - exactly one of mMap / mParcelledData will be null
- // (except inside a call to unparcel)
- /* package */ Map<String, Object> mMap = null;
- /*
- * If mParcelledData is non-null, then mMap will be null and the
- * data are stored as a Parcel containing a Bundle. When the data
- * are unparcelled, mParcelledData willbe set to null.
- */
- /* package */ Parcel mParcelledData = null;
- public void putFloat(String key, float value) {
- unparcel();//首先解析出mParcelledData到mMap
- mMap.put(key, value);
- }
- /* package */ 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;//回收以后值為null
- }
