Thursday, March 21, 2013

Example to implement Parcelable

Parcel is a class container for a message (data and object references) that can be sent through an IBinder. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC (using the various methods here for writing specific types, or the general Parcelable interface), and references to live IBinder objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel.

Parcelable is 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.

Modify last exercise "Perform background processing with IntentService" to pass extra as our custom Parcelable.

Extra passed as Parcelable

Create our custom class, implements Parcelable.
package com.example.androidintentservice;

import android.os.Parcel;
import android.os.Parcelable;

public class MyParcelable implements Parcelable {
 public String blogName;
 public String blogAddress;
 public static final Parcelable.Creator<MyParcelable> CREATOR =
   new Parcelable.Creator<MyParcelable>(){

    public MyParcelable createFromParcel(Parcel source) {
     return new MyParcelable(source);

    public MyParcelable[] newArray(int size) {
     return new MyParcelable[size];
 public MyParcelable(){
 public MyParcelable(Parcel source){

 public int describeContents() {
  return 0;

 public void writeToParcel(Parcel dest, int flags) {
 public void readFromParcel(Parcel source){
  blogName = source.readString();
  blogAddress = source.readString();


Modify main activity, to pass Extra as MyParcelable.
package com.example.androidintentservice;

import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView textResult;
 private MyBroadcastReceiver myBroadcastReceiver;

 protected void onCreate(Bundle savedInstanceState) {
  textResult = (TextView)findViewById(;

  //prepare MyParcelable passing to intentMyIntentService
  MyParcelable myParcelable = new MyParcelable();
  myParcelable.blogName = "Android-er";
  myParcelable.blogAddress = "";
  //Start MyIntentService
  Intent intentMyIntentService = new Intent(this, MyIntentService.class);
  intentMyIntentService.putExtra(MyIntentService.EXTRA_KEY_IN, myParcelable);
  myBroadcastReceiver = new MyBroadcastReceiver();
  //register BroadcastReceiver
  IntentFilter intentFilter = new IntentFilter(MyIntentService.ACTION_MyIntentService);
  registerReceiver(myBroadcastReceiver, intentFilter);
 protected void onDestroy() {
  //un-register BroadcastReceiver

 public class MyBroadcastReceiver extends BroadcastReceiver {

  public void onReceive(Context context, Intent intent) {
   String result = intent.getStringExtra(MyIntentService.EXTRA_KEY_OUT);



Modify to receive MyIntentService.
package com.example.androidintentservice;

import android.content.Intent;

public class MyIntentService extends IntentService {
 public static final String ACTION_MyIntentService = "com.example.androidintentservice.RESPONSE";
 public static final String EXTRA_KEY_IN = "EXTRA_IN";
 public static final String EXTRA_KEY_OUT = "EXTRA_OUT";
 MyParcelable parcelableIn;
 String extraOut;

 public MyIntentService() {

 protected void onHandleIntent(Intent intent) {
  //get input
  parcelableIn = intent.getParcelableExtra(EXTRA_KEY_IN);
  extraOut = "Passed as Parcelable:\n" 
    + parcelableIn.blogName + "\n" 
    + parcelableIn.blogAddress;
  //dummy delay for 5 sec
  try {
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
  } //wait 3 sec
  //return result
  Intent intentResponse = new Intent();
  intentResponse.putExtra(EXTRA_KEY_OUT, extraOut);


download filesDownload the files.

No comments: