android Parcelable完全解析

來源:互聯網
上載者:User

android Parcelable完全解析

title: android.os.Parcelable文檔自譯 date: 2014-12-05 09:16:06

tags:

android.os.Parcelable介面的已知間接子類很多,這裡就不列舉了:

Class Overview

 

Interface for classes whose instances can be written to and restored from a

Parcel. Classes implementing the Parcelable interface must also have a static

field called CREATOR, which is an object implementing the Parcelable.Creator

interface.

該Interface用於執行個體可以被寫入,並且可以從Parcel中恢複的classes(對於Parcel暫時只要知道它是一個存 放資料的容器就好了,我會在android IPC中寫到它)。實現android.os.Parcelable介面的類必須持有一個實 現了android.os.Parcelable介面的名為 CREATOR 的靜態欄位。

android.os.Parcelable介面典型使用方式,這是官網的例子:

package me.androiddemo.canglangwenyue.androiddemo;

 

import android.os.Parcel;

import android.os.Parcelable;

 

/**

* Created by canglangwenyue on 12/5/14.

*/

public class MyParcelable implements Parcelable {

private int mData;

 

public int describeContents() {

return 0;

}

 

public void writeToParcel(Parcel out, int flags) {

out.writeInt(mData);

}

 

public static final Parcelable.Creator CREATOR

= new Parcelable.Creator() {

public MyParcelable createFromParcel(Parcel in) {

return new MyParcelable(in);

}

 

public MyParcelable[] newArray(int size) {

return new MyParcelable[size];

}

};

 

private MyParcelable(Parcel in) {

mData = in.readInt();

}

}

Summary(簡介)

Nested Classes(嵌套類)

1.interface Parcelable.ClassLoaderCreator

Specialization of Parcelable.Creator that allows you to receive

the ClassLoader the object is being created in.

 

解析:專業化的 Parcelable.Creator,允許你接收的Object內部建立的 ClassLoader 對象.

 

2.interface Parcelable.Creator

Interface that must be implemented and provided as a public CREATOR field that

generates instances of your Parcelable class from a Parcel.

解析:該介面必須被子類實現,而且CREATOR 作為公有欄位來提供,CREATOR 用於從 Parcel中執行個體化你的可封裝類.

Constants(常量)

1.int CONTENTS_FILE_DESCRIPTOR

Bit masks for use with describeContents(): each bit represents a kind of object

considered to have potential special significance when marshalled.

 

解析:用於 describeContents() 的位元遮罩,每一位代表它編組時附加的特殊含義。

 

2.int PARCELABLE_WRITE_RETURN_VALUE

Flag for use with writeToParcel(Parcel, int): the object being written is a

return value, that is the result of a function such as Parcelable

someFunction(), void someFunction(out Parcelable), or void

someFunction(inout Parcelable).

 

解析:writeToParcel(Parcel, int)的標誌位:作為一個傳回值,是Parcelable

someFunction(), void someFunction(out Parcelable), or void

someFunction(inout Parcelable)返回的result。

Public Methods(公用method)

1.abstract int describeContents()

Describe the kinds of special objects contained in this Parcelable's marshalled

representation.

 

解析:描述各種特殊對象,它們包含在可封裝對象的編組形式中.

 

2.abstract void writeToParcel(Parcel dest, int flags)

Flatten this object in to a Parcel.

 

解析:將該對象展開到Parcel(存放資料的容器)中。

Parcelable適用於通過Intent來傳遞自訂對象。最後給出一個用Parcelable進行資料傳送的例子

1.發送Object的Activity,內容很簡單,點擊Button,Intent攜帶Object跳轉到MainActivity2(用來接收Object的Activity)。

package me.androiddemo.canglangwenyue.androiddemo;

 

import android.content.Intent;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

 

import java.util.HashMap;

 

/**

* @author canglangwenyue

* 用來發送資訊的Activity

*/

 

public class MainActivity extends ActionBarActivity {

 

private Button sendButton;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

 

sendButton = (Button) findViewById(R.id.send_button);

 

sendButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent();

Person person = new Person();

 

person.name = WenYue;

intent.putExtra(WenYue,person);

intent.setClass(MainActivity.this,MainActivity2.class);

 

startActivity(intent);

}

});

 

}

 

 

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_main, menu);

return true;

}

 

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

 

//noinspection SimplifiableIfStatement

if (id == R.id.action_settings) {

return true;

}

 

return super.onOptionsItemSelected(item);

}

}

2.MainActivity2用來接收來自MainActivity的Object,並列印Person.name的長度和content。

package me.androiddemo.canglangwenyue.androiddemo;

 

import android.content.Intent;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;

 

/**

  • @author canglangwenyue
  • 接收資料的Intent */
    public class MainActivity2 extends ActionBarActivity {
    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_activity2);
    Intent intent = getIntent();
  •  
  • Person person = intent.getParcelableExtra(WenYue);
  •  
  • Log.e(MainActivity2 received message's length----->,String.valueOf(person.name.length()));
  • Log.e(MainActivity2 received message content------>,person.name);

  • }
    @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main_activity2, menu); return true; }
    @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId();
    //noinspection SimplifiableIfStatement
  • if (id == R.id.action_settings) {
  • return true;
  • }
  •  
  • return super.onOptionsItemSelected(item);

  • } }

    3.Parcelable的實現,具體細節前面已經講到了,就不多說了。

    package me.androiddemo.canglangwenyue.androiddemo;

     

    import android.os.Parcel;

    import android.os.Parcelable;

     

    /**

    * Created by canglangwenyue on 12/5/14.

    * Parcelable常用於Intent中進行自訂對象的傳遞

    */

    public class Person implements Parcelable {

     

     

    public String name;

     

    @Override

    public int describeContents() {

    return 0;

    }

     

    @Override

    public void writeToParcel(Parcel dest, int flags) {

     

    dest.writeString(name);

     

    }

     

    /*

    需要重寫Creator實現android.os.Parcelable介面的類必須持有一個實現了android.os.Parcelable介面的名為 CREATOR 的靜態欄位

    */

     

    public static final Creator CREATOR = new Parcelable.Creator() {

     

    /*

    重寫Creator方法

    */

    @Override

    public Person createFromParcel(Parcel source) {

    Person person = new Person();

    person.name = source.readString();

    return person;

    }

     

    @Override

    public Person[] newArray(int size) {

    return new Person[0];

    }

     

    };

     

    }

    最後給出在MainActivity2中log列印的結果,眼見為實,哈哈:

     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.