Skip to content

Custom Intents and Broadcasting with Receivers

February 2, 2010

Hey everyone,

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:

public class OutgoingReceiver extends BroadcastReceiver {

    public static final String CUSTOM_INTENT = "jason.wei.custom.intent.action.TEST";

    public void onReceive(Context context, Intent intent) {
        System.out.println("HIT OUTGOING");
        Intent i = new Intent();


The Second Receiver:

public class IncomingReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(OutgoingReceiver.CUSTOM_INTENT)) {
            System.out.println("GOT THE INTENT");

The Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <receiver android:name=".OutgoingReceiver" android:enabled="true">
				<action android:name="android.intent.action.PHONE_STATE"></action>
		<receiver android:name=".IncomingReceiver" android:enabled="true">
				<action android:name="jason.wei.custom.intent.action.TEST"></action>

    <uses-sdk android:minSdkVersion="3" />

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

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:

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

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.

As a proof of concept, here’s a screen shot of my log:

And so we see that once the call is initiated, it triggers the OutgoingReceiver and prints the first line, which then triggers the second broadcast intent, hits our IncomingReceiver, and prints the second line (in this order).

Of course, this is just the skeleton of how we can create custom intents and allow receivers to interact with one another but in your application you can create multiple receivers which get all intercept this intent, and in them you can extract different sets of data and perform different sets of operations, etc.

Hope this was helpful! Happy coding.

– jwei

62 Comments leave one →
  1. warrior permalink
    February 4, 2010 2:58 am

    Thanks a lot..!!It was very helpfull..!!n was pretty easy for a beginer like me to understand.!!thanx a lot..!!

  2. Marco permalink
    April 27, 2010 1:48 am

    Thanks for this example! Was a great help 🙂

  3. Ethan permalink
    April 28, 2010 2:19 pm

    Great example! One of the very few that I found that was clearly stated, but I have some seemingly basic questions:

    Where do I put the two receiver classes? Are the receiver classes in separate files? Are they inner classes of my main Activity class? I’m very new to both Java and Android and think I’m missing some basic concepts here. Thanks in advance.

    • April 29, 2010 4:50 pm

      Hey Ethan,

      I’d put them as separate classes and then declare them in your AndroidManifest file.

      – jwei

      • Ethan permalink
        May 5, 2010 10:50 pm

        Thanks so much. Your example worked perfectly and I set up an alarm successfully using your example as a guide. Your whole site is very well thought out.

        Thanks again for your efforts,

      • May 5, 2010 11:49 pm


        Thanks Ethan

  4. June 6, 2010 6:07 am

    Can you publish a zip file of it all?

    I can’t get my BroadcastReceiver to work, and a complete “gotta work” example like this would be fantastic.


    • June 6, 2010 7:46 am

      Hey Jim,

      What exactly isn’t working? The example I provided should be “complete”.

      – jwei

    • Ethan permalink
      June 6, 2010 8:16 am


      I think what you are missing is simply an Activity.

      You need to set up an activity that creates an intent and then sends an intent using sendBroadcast(intent). I used an alarmmanager, so mine is a bit different, but I guess you want something like this in your activity:

      Intent intent = new Intent(mContext, OutgoingReceiver.class);
      intent.putExtra(“someKey”, “someValue”);

      JWei, correct me I’m wrong here. My alarm manager worked fine, but I’d love to know if I really understand.


      • June 7, 2010 8:08 am

        Thanks jwei512, Ethan. I have been desperately scouring for a solution…

        Here’s the big picture.
        I want to show the speed limit in a widget.
        I want to update it when the speed limit changes (I have that code working).
        AlarmManager won’t work since its not a timed event…
        So the thought was, create two items, 1=widget, 2=status-bar-app
        (that’s why I get lazy and beg people for complete project zip-files, since I am kind of new at Android and the simplest things trip me up…)

        I never get this Intent.

        The status bar app does this:
        Uri uri = Uri.parse(“55”);
        Intent intent = new Intent(“org.jamesp.gpscruise.UpdateWidgetAction”, uri);

        The widget does this:

        public class MyIntentReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
        System.out.println(“GOT THE INTENT”);

  5. June 7, 2010 8:09 am

    forgot this part

    • June 7, 2010 8:10 am

      hmm, is html grabbing my manifest?? I will post the intent here again…

  6. June 7, 2010 8:11 am

    ok, I took out the braces….

    receiver android:name=”.MyIntentReceiver” android:label=”@string/widget_name”
    action android:name=”org.jamesp.gpscruise.UpdateWidgetAction”

  7. Ethan permalink
    June 7, 2010 8:25 am


    I don’t know if “55” is a valid URI, but that probably shouldn’t matter, but I noticed you don’t have android:enabled=”true” in the manifest like JWei does.


    • June 20, 2010 4:55 pm

      i need to make a forum haha

      hopefully this problem was settled?

      – jwei

  8. Sarada permalink
    June 8, 2010 12:58 am

    Hi jwei,

    How can we set data to custom actions? I used the below code snippet but it did not work. I am unable to receive data in other activity.

    Intent i=new Intent();
    i.setData(Uri.parse(“android.resource://” + R.string.button));

    I defined the below activity in the manifest file.

    Can I pass data to other activity? If so, can I pass it through setData() method with out using putExtras() ?

    • June 20, 2010 5:01 pm

      Hey Sarada,

      So I think your problem lies here:

      Uri.parse(“android.resource://” + R.string.button)

      You have to be careful that R.string.button is actually an integer that just contains the id of the string “button” defined in your strings.xml file.

      Thus, when you do something like concatenating strings the way you just did it, you’re actually creating a string that looks like:


      Which I think is not what you’re going for? If I’m wrong then maybe you want to use the i.setStringExtra() method instead of i.setData() method.

      – jwei

  9. Jody Grady permalink
    June 15, 2010 9:11 pm

    Brilliant! That captured in one page more than the past 50 websites I’ve stared at trying to pass file path to a BroadcastReceiver that processes incoming text messages and needs to receive path changes from the main Activity. I wish I had found this 10 days ago.

  10. bill permalink
    June 25, 2010 4:33 pm

    I have a receiver that I can not figure out how to get the shared preferences from the main activity. I have an activity that allows the user to enter a phrase that when an sms comes in and matches that phrase will ring the phone. My broadcast receiver class extends broadcast receiver. I just cant figure out how to get the shared preferences set in the main activity. hope that makes sense.

  11. Jonathan permalink
    July 21, 2010 1:41 pm

    Great example! Will this work if the broadcast receiver is in a different package than the class sending the custom intent?

    Let me explain: I’m trying to set up a service that broadcasts custom intents for anyone who wants to make an app that will listen, but I’m having trouble getting it to work. Will this example work? (I’m not having any luck making it work.) If not, does anyone know where I can find any examples of this?

    • July 22, 2010 4:54 am

      So just to be clear – you want to broadcast an intent that other applications installed on the phone can receive right?

      Using this strategy of registering a custom intent and then broadcasting it, other applications should be able to receive it.

      Just as how the Android OS broadcasts intents (i.e. intent android.intent.action.BATTERY_CHANGED) which any application can intercept, I don’t see how your application broadcasting an intent would be any different. Of course I may be wrong and the native intents from the system may have special properties that allow them to be intercepted across any application, this doesn’t seem like the case.

  12. Sergio permalink
    September 13, 2010 7:07 pm

    I want to be able to make my activity do something each time it receives an SMS, however, I have set the receiver, but I cant get it to work.


  13. Sergio permalink
    September 13, 2010 7:08 pm
    <receiver android:name=".SmsReceptor" android:enabled="true">
        <action android:name="android.provider.Telephony.SMSRECEIVED" /> 
  14. Patrick permalink
    September 29, 2010 1:14 pm

    So basically, I am wrinting (or trying to) an app that receives an SMS and then uses a different class to send data over my wifi connection. I’ve been trying to figure out how to make the SmsReceiver class start the MessageSender class. From reading above, this is the best description of what “fits the bill” of what I am trying to do.
    It looks like I need to register the sms_received intent and my_own_custom intent in the manifest, extend both activities to the broadcast receiver, and use

    public static final String CUSTOM_INTENT = “my_own_custom”;

    in the SmsReceiver class. Then I would make the MessageSender class something like this:

    public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(SmsReceiver.CUSTOM_INTENT))

    Is that right or should I look elsewhere?

    • October 4, 2010 11:07 am

      Hey Patrick,

      So I think the flow of what you’re trying to do would look something like this…

      1) Have a Receiver class that listens for the SMS_received intent.

      2) Upon intercepting that intent, you could simply start a Service/Activity that is the MessageSender class, or if you want to use a broadcast you could broadcast your own custom intent (i.e. “message_sender_custom_intent” or something) and broadcast that.

      3) If you choose to do the latter, then you would need a 3rd broadcast receiver that has an intent filter set to catch the custom intent. Once you receive the custom intent you can then start sending data over your wifi connection as desired.

      Hope this helped.

      – jwei

  15. Brandon permalink
    November 2, 2010 11:56 pm

    thank alot for this but i have a problem, can this be use for sending intents from Broadcast Receiver to Activity?? i appreciate ur help

    • November 3, 2010 12:03 am

      You can start Activities from Receivers and when starting those Activities you can send data through the intent.

      Otherwise, try looking into the Service class and seeing if there’s a way to receive an intent and update something without having to start a new Activity.

      – jwei

  16. Mauladi permalink
    March 1, 2011 1:15 am

    Hi jwei,
    howto send data from a service to another activity?

    • May 2, 2011 1:44 am

      Hey Mauladi,

      Same way you would send it from Activity to Activity, just through an Intent and passing that intent to the startActivity() method.

      – jwei

  17. Sudeep permalink
    April 1, 2011 9:15 pm

    Hello,this tutorial was good.I am doing an android app for my mini-project.I want to give some statistics about call.This activity should come up when a phone call is made or phone call is ended.Is this possible..?If so,can u briefly explain??I would be thankful to you if you help me out.

  18. bhushan permalink
    April 20, 2011 6:30 am

    chek out this with example of Broadcast reciver

  19. sathish permalink
    May 4, 2011 2:12 am


    You have done a greate job , it is very understandable for beginners like me , thanks a lot for the greate post.

    Also i would like to know broadCasting of intents…
    i have a scenario like , in an app , having 3 tabs and in first tab i would navigate 3 activities , same for other tabs also,

    initially fisrt tab tied with one activity then i need to push a new activity into the same tab .
    if it is possible by sending broadcastintent then please explain me with some suitable example..

    thanks for your greate work in helping me out


  20. June 10, 2011 11:44 pm


  21. Avtar Guleria permalink
    August 25, 2011 3:06 am

    Nice post

  22. September 26, 2011 1:43 am

    Hi everyone,
    I’ve got difficult for me question.
    When I open mail client and I see phone number then I press it. After that I want to catch in BroadcastReceiver action that is being called by Android. Shortly – I want to swap it with my own Activity.
    Any ideas ?

  23. Chirag permalink
    October 18, 2011 9:46 pm

    Very nice example

  24. helbert permalink
    October 26, 2011 1:05 pm

    great! thanks a lot!

    genial, muchisimas gracias!

  25. tuan permalink
    December 1, 2011 5:04 am

    it’s great

  26. December 5, 2011 1:08 am

    Thanks 🙂

    clearly stated..

    I did not find some thing this much straight forward.

  27. Jyothi permalink
    December 8, 2011 2:51 am

    Tanks a lot… actually …some how “Notification.FLAG_INSISTENT” is not working for some small notification sound like “beep” …so i am planning to use your idea and do a work around for my problem… thanks a lot

  28. December 13, 2011 6:11 pm

    Nice post, Thank’s

  29. Anuj permalink
    January 23, 2012 8:03 am


  30. permalink
    March 19, 2012 7:43 am

    Excellent web site. Plenty of useful information here. I am sending it to several pals ans additionally sharing in delicious. And obviously, thank you to your effort!

  31. March 24, 2012 4:20 am

    Thanks for this nice example.

  32. Arslan permalink
    March 29, 2012 7:56 pm

    Simply straightforward way to explain BroadcastReceivers. Thanks

  33. Joe permalink
    May 7, 2012 8:59 am

    Thanks! You helped me catch a bug in my code.

  34. Amit permalink
    June 6, 2012 5:09 am

    tahnks man…..

  35. Rich permalink
    July 23, 2012 5:30 am

    How does your example change if I want to create a custom receiver that has nothing to do with the phone? I want to set up a broadcast intent that keeps track of a wifi connection, and the device I’ll be running on has no phone.

    • July 24, 2012 11:26 am

      Hey Rich,

      It seems like you would need the WifiManager class. Here’s a good example of how you would do this:

      So basically, you would set up a broadcast receiver that listens for those intents above. I’m not sure what you mean by the device it’s running on “has no phone”, but as long as you’re on the Android OS this should work independent of device.

      Let me know if that helps.

      – jwei

      • Rich permalink
        July 24, 2012 11:29 am

        We’re supplying a complete system to our customers. The software will be running on a Samsung Galaxy Tab 2, which has no cellular hardware. I need to make something that will alert the user if he loses Wifi connectivity. Thanks for the help.

  36. muying permalink
    July 26, 2012 8:16 pm

    Wow!! I spent 2 hrs on understanding how does this “receiver” works..and your post show me how simple it is!! Thanks

  37. shailendra permalink
    December 13, 2012 8:14 am

    helped me to understand how i can create my own intent-action.
    however Android doc says ” Those you invent should include the application package as a prefix” , so i think in your case it should be “jason.wei.apps.androidtest.TEST”

  38. aneela permalink
    December 16, 2012 9:44 pm

    if I want the second broadcast Intent to be received by my activity then what should I do to register it with the activity..??
    I want as soon as this intent is received by my activity, its onResume() method get called.

  39. February 2, 2013 12:44 pm

    Nice one……….

    Also found good tutorial on


  40. Mayaa permalink
    March 19, 2013 9:27 am

    Really helpful post!! Thanks. Even this website also addresses something similar.. Have a look.. Might help..

  41. Venkatraman permalink
    May 8, 2013 2:42 am

    This sample is good for custom broadcast intent within application. But if we want to use this custom intent on the another app what would be the methodology… ie. to make our custom intent action as android global intent action


  1. Tweets that mention Custom Intents and Broadcasting with Receivers « Think Android --
  2. Farhan's Blogs & Portal
  3. » Blog Archive » Android Helpful links
  4. What is the name of the Activity that is called when the phone is ringing | PHP Developer Resource
  5. One update for widget « M. L. B.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: