Search...

Saturday, December 3, 2011

How to create Custom Intents and Broadcasting with Receivers in android

Here’s an example that tries to satisfy “Warrior’s” request on sending custom intents and grabbing them with broadcast receivers. This example has two receivers and an android manifest file:
The First Receiver:

view sourceprint?
01 public class OutgoingReceiver extends BroadcastReceiver { 
02   
03     public static final String CUSTOM_INTENT = "jason.wei.custom.intent.action.TEST"; 
04   
05     @Override
06     public void onReceive(Context context, Intent intent) { 
07         System.out.println("HIT OUTGOING"); 
08         Intent i = new Intent(); 
09         i.setAction(CUSTOM_INTENT); 
10         context.sendBroadcast(i); 
11     } 
12   
13 }
The Second Receiver:

view sourceprint?
1 public class IncomingReceiver extends BroadcastReceiver { 
2   
3     @Override
4     public void onReceive(Context context, Intent intent) { 
5         if (intent.getAction().equals(OutgoingReceiver.CUSTOM_INTENT)) { 
6             System.out.println("GOT THE INTENT"); 
7         } 
8     } 
9 }
The Android Manifest:

view sourceprint?01 <?xml version="1.0" encoding="utf-8"?> 
02 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
03       package="jason.wei.apps.androidtest"
04       android:versionCode="1"
05       android:versionName="1.0"> 
06     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
07         <receiver android:name=".OutgoingReceiver" android:enabled="true"> 
08             <intent-filter> 
09                 <action android:name="android.intent.action.PHONE_STATE"></action> 
10             </intent-filter> 
11         </receiver> 
12         <receiver android:name=".IncomingReceiver" android:enabled="true"> 
13             <intent-filter> 
14                 <action android:name="jason.wei.custom.intent.action.TEST"></action> 
15             </intent-filter> 
16         </receiver> 
17   
18     </application> 
19     <uses-sdk android:minSdkVersion="3" /> 
20   
21 <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> 
22 </manifest>

So here’s a quick explanation of what’s going on in this code snippet. So notice that we first define our custom intent as:

view sourceprint?
1 public static final String CUSTOM_INTENT = "jason.wei.custom.intent.action.TEST";
This is the intent’s action (i.e. typically this would be something like android.intent.action.ACTION_VIEW) and you can think of this as our way of “tagging” the intent and characterizing it. Notice that you can also attach data and extras to this intent as usual (see http://thinkandroid.wordpress.com/2009/12/26/passing-info-between-activities/).
Now, in our manifest, our intent filter only allows for intents with actions “jason.wei.custom.intent.action.TEST” to be received and so technically we don’t even need to do the check in the IncomingReceiver (but it never hurts).
The flow of our little example works like this – first we define the OutgoingReceiver to intercept all intents broadcasted with action READ_PHONE_STATE (i.e. phone calls, this is how we trigger the first receiver). Once the OutgoingReceiver intercepts the first broadcasted intent, it will broadcast the second custom intent using the context.sendBroadcast() method which will then be received by our second receiver IncomingReceiver.

No comments:

Post a Comment