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.
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://arteluzevida.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 the files.