Saturday, August 15, 2009

Inter Process Communication in Android through Intent

In Android, one of the nice ways for passing data between different processes is through the help of Intent. In the following example i have tried to explain it by two different applications. One application is called the IntentSupplier. This is started from another application called IntentExample. The application IntentSupplier passes some string data to the IntentExample app through the help of an Intent object which the IntentSupplier application displays in a message box.

The IntentSupplier application looks like the following.




When we click one of the buttons it passes its string data to the IntentExample which looks like the following.



The source code for IntentSupplier is as follows:

package android.training.intentsupplier;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class IntentSupplier extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button1 = (Button)findViewById(R.id.Button01);

Button button2 = (Button)findViewById(R.id.Button02);

button1.setOnClickListener(onClickButton1);
button2.setOnClickListener(onClickButton2);

}

private OnClickListener onClickButton1 = new OnClickListener() {

public void onClick(View v){

if (v.getId() == R.id.Button01){

returnResult("Message1 coming from IntentSupplier");
}
}
};

private OnClickListener onClickButton2 = new OnClickListener() {

public void onClick(View v){

if (v.getId() == R.id.Button02){

returnResult("Message2 coming from IntentSupplier");
}
}
};

void returnResult(String msg) {

Intent i = new Intent();

i.putExtra("android.training.intentsupplier.resultfromintentsupplier", msg);

setResult(RESULT_OK, i);

finish();
}

}


Look at the function

void returnResult(String msg).

Here at the line i.putExtra("android.training.intentsupplier.resultfromintentsupplier", msg), we are basically parceling the data (msg) and giving it a label called resultfromintentsupplier. Please see how the package info is prefixed with this label.

The source code for the IntentExample is as follows:

package android.training.intentexample;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class IntentExample extends Activity {

static final int REQUEST_CODE = 1001;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Intent getMessageFromIntentSupplier = new Intent();

getMessageFromIntentSupplier.setClassName("android.training.intentsupplier", "android.training.intentsupplier.IntentSupplier");

getMessageFromIntentSupplier.setAction("android.training.intentsupplier.android.intent.action.INTENTSUPPLIERTEST");

getMessageFromIntentSupplier.addCategory("CATEGORY_DEFAULT");

getMessageFromIntentSupplier.setType("vnd.example.greeting/vnd.example.greeting-text");

try {
startActivityForResult(getMessageFromIntentSupplier,REQUEST_CODE);

}

catch(ActivityNotFoundException e) {
Log.e("IntentExample", "Activity could not be started...");
}

}
}

public void onActivityResult(int requestcode, int resultcode, Intent result ) {

if(requestcode == REQUEST_CODE){
if(resultcode == RESULT_OK){
message = result.getStringExtra("android.training.intentsupplier.resultfromintentsupplier");

new AlertDialog.Builder(this)
.setTitle("Msg from Intent Supplier")
.setMessage(message)
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
// do nothing – it will close on its own
}
})
.show();
}
}
}

}

The AndroidManifest.xml file of the IntentSupplier application looks like the following:

(** Please replace the "(" & ")" brackets with the angular brackets to xmlize the file contents)

(?xml version="1.0" encoding="utf-8"?)
(manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.training.intentsupplier"
android:versionCode="1"
android:versionName="1.0")
(application android:icon="@drawable/icon" android:label="@string/app_name")
(activity android:name=".IntentSupplier"
android:label="@string/app_name")

(intent-filter)
(action android:name="android.intent.action.MAIN" /)
(category android:name="android.intent.category.LAUNCHER" /)
(/intent-filter)

(intent-filter)
(action android:name="android.intent.action.INTENTSUPPLIERTEST" /)
(category android:name="android.intent.category.DEFAULT" /)
(data android:mimeType="vnd.example.greeting/vnd.example.greeting-text" /)
(/intent-filter)
(/activity)
(/application)
(uses-sdk android:minSdkVersion="3" /)
(/manifest)

The main.xml file of the IntentSupplier application looks like the following:

(?xml version="1.0" encoding="utf-8"?)
(LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
)
(TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/)
(Button android:text="Message1" android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content")(/Button)
(Button android:text="Message2" android:id="@+id/Button02" android:layout_width="fill_parent" android:layout_height="wrap_content")(/Button)
(/LinearLayout)

In this example we have started the IntentSupplier app through the function startActivityForResult. It then waits for the result from the child activity, and once the result arrives, the callback function onActivityResult is called. in this function we extract the data sent from the child activity (by using the function getStringExtra) and show it in a message box.

To run this application we need to run once the IntentSupplier application first.

Hope this discussion becomes helpful for the newbies of Android.

4 comments:

Senjuti Roy said...

Hi,

I am very new to android. I have tried this example on my machine but it didn't work. Do I need to modify manifest file in any way or the main.xml of the IntentSupplier and IntentExample?

Somenath said...

Yes, we need to modify the AndroidManifest.xml file of the IntentSupplier. Its now published in the blog... you can have a look at it...

Unknown said...

Thanks a lot for the tutorial . saved me hours.

Unknown said...

thanks a lot for the turorial. saved me hours