Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Intercommunication amongst Applications

Author: Rachee Singh
Published? false
FormatLanguage: WikiFormat

Problem:

You need to get different applications deployed on a device to communicate. For instance, the running application wishes to notify another application (which is in the background) of a certain event.

Solution:

To initiate communication, an application broadcasts an intent. The applications in the background that register a receiver for that particular intent will receive it and perform the necessary action in their BroadcastReceiver subclass.

Discussion:

To broadcast an intent from your application, insert the following code:

       Intent intent = new Intent("com.bluetooth.LINK_BROKEN"); 
       sendBroadcast(intent);
       Log.d("My Class", "Intent Sent");

The application which wishes to be notified of an action from your application, needs the following code:

public class BluelinkIntents extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
	         Toast.makeText(context, "Application X Received the Intent!", oast.LENGTH_SHORT).show();
		Log.d("Application X", "Received the Intent!");
	}

}

This code outlines a separate class which extends BroadcastReceiver. In this class, the application being notified can perform tasks once the intent is recieved.

These few lines of code have to be added to the AndroidManifest of the application which is being notified.

<!-- Receiver for Intent -->
        <receiver android:name="org.openintents.intents.BluelinkIntents" android:enabled="true" >
 		<intent-filter>
                <action android:name="com.bluetooth.LINK_BROKEN" />
                <category android:name="android.intent.category.DEFAULT" />
		</intent-filter>
		</receiver>

With all this set up, when your application starts, it will broadcast an intent. All the other applications present in the background on that Android device and which have the BroadcastReceiver set up will receive the intent and display a toast 'Application X Received the Intent!'.

idarwin 2011-09-24 09:25:37.109 The Solution section talks about theft but the code example uses Bluetooth, Could you please make it consistent, Maybe define own intent in one app and use the string in Manifest? Thx!