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 MyParcelable.java, 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>(){

    @Override
    public MyParcelable createFromParcel(Parcel source) {
     return new MyParcelable(source);
    }

    @Override
    public MyParcelable[] newArray(int size) {
     return new MyParcelable[size];
    }
 };
 
 public MyParcelable(){
 }
 
 public MyParcelable(Parcel source){
  readFromParcel(source);
 }

 @Override
 public int describeContents() {
  return 0;
 }

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

}


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

import android.os.Bundle;
import android.app.Activity;
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;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textResult = (TextView)findViewById(R.id.result);

  //prepare MyParcelable passing to intentMyIntentService
  MyParcelable myParcelable = new MyParcelable();
  myParcelable.blogName = "Android-er";
  myParcelable.blogAddress = "http://android-er.blogspot.com/";
  
  //Start MyIntentService
  Intent intentMyIntentService = new Intent(this, MyIntentService.class);
  intentMyIntentService.putExtra(MyIntentService.EXTRA_KEY_IN, myParcelable);
  startService(intentMyIntentService);
  
  myBroadcastReceiver = new MyBroadcastReceiver();
  
  //register BroadcastReceiver
  IntentFilter intentFilter = new IntentFilter(MyIntentService.ACTION_MyIntentService);
  intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
  registerReceiver(myBroadcastReceiver, intentFilter);
 }
 
 @Override
 protected void onDestroy() {
  super.onDestroy();
  //un-register BroadcastReceiver
  unregisterReceiver(myBroadcastReceiver);
 }

 public class MyBroadcastReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
   String result = intent.getStringExtra(MyIntentService.EXTRA_KEY_OUT);
   textResult.setText(result);
  }

 }

}


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

import android.app.IntentService;
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() {
  super("com.example.androidintentservice.MyIntentService");
 }

 @Override
 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 {
   Thread.sleep(5000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } //wait 3 sec
  
  //return result
  Intent intentResponse = new Intent();
  intentResponse.setAction(ACTION_MyIntentService);
  intentResponse.addCategory(Intent.CATEGORY_DEFAULT);
  intentResponse.putExtra(EXTRA_KEY_OUT, extraOut);
  sendBroadcast(intentResponse);
 }

}


download filesDownload the files.

No comments:

Post a Comment