B4A Library BroadcastReceiver

Discussion in 'Additional libraries, classes and official updates' started by XverhelstX, Nov 9, 2011.

  1. XverhelstX

    XverhelstX Well-Known Member Licensed User


    I'm proud to announce another library of mine: BroadcastReceiver.

    I will try to explain as hard as I can, as I didn't went in to deep in broadcasting and stuff.

    So, the Android Operating System sends all kind of messages around where the normal users can't see thse. These are called Intents. Every intent has a special role in the Android OS. But the intents don't stay there, wandering around. This is where the BroadcastReceiver comes in. BroadcastReceivers picks up the intents that you need with an intentFilter. This way, you can start actions when something happens in your phone.
    But not only can you receive intents, You can also send/broadcast your own intents in your phone, or even pass your own info to another app that it listening.

    (please tell me if i'm somewhere wrong ;)) I only learned about broadcasting and intents through this library.

    Let me give you an example.

    When a SMS text message comes into your phone, the intent "android.provider.Telephony.SMS_RECEIVED" is raised and send "through" your phone. Your standard text application (or even ChompSMS, GoSMS PRO, ...) picks the intent up with a BroadcastReceiver, and displays the incoming message on the notification bar and inbox, etc.

    But how does it work now with the BroadcastReceiver Lib?
    No manually changing the Android Manifest file is needed!
    You only need a service to start working. (and an activity of course to start your service)

    In your service process globals, declare the BroadcastReceiver;

    Dim Broadcast As BroadcastReceiver
    I hope you know what this does, otherwise, I recommend you to read Klaus' Beginners Tutorial.

    In the service_create, we start by initializing the library.

    The parameter "BroadcastReceiver" is needed for calling a sub when an intent arrives. I will explain this later.

    After the services has been created and the library has been initialized, we start setting up our receiver that will listen for intents. This is done in Service_start.

    Sub Sub Service_Start (StartingIntent As Intent)
    End Sub
    - AddAction will add the intent you are listening for in a filter. So when a message comes in, "android.provider.Telephony.SMS_RECEIVED" is broadcasted and received.
    - SetPriority sets the priority of the listening broadcast. The higher the value, the more important the incoming intent will be thus the quicker it will be called instead of other apps with a lower priority.
    - registerReceiver starts your receiver and listening for intents/actions. You can add an extra addAction parameter to the filter.

    Now you are done setting up your basic receiver, but now something should be called when a sub arrives: the onreceive method. This is called with the action string of the intent you are listening for.
    As I mentionned before, under here starts with BroadcastReceiver, this is the eventname parameter you gave in earlier in the initialize method.

    Sub BroadcastReceiver_OnReceive (Action As String)
    'can only abort when sendOrderedbroadcast is called.
    End Sub
    The toastmessage, in this case will show android.provider.Telephony.SMS_RECEIVED if an sms has been received.
    Next, we stop the broadcast with AbortBroadcast so no other applications will receive this intent. AbortBroadcast will only be called if the broadcast sended is an Ordered one. You can check this with:

    This is basically IT for receiving broadcasts.
    Now, there is 1 more thing you should know.

    - You can send a broadcast in 2 ways.
    Ofcourse instead of Broadcast.SMS_RECEIVED, you can put any string in there that you want.

    The difference between these two is that when you send an ordered broadcast, the broadcast will be more accurate filtered on the android permission, and as I said, you can use AbortBroadcast on this one.
    You could just open your Android Manifest File and see what permissions your app has.

    There are still some more functions, but these are explained in the library itself.

    If you want to find info about intents, you can find all of them here:

    Some Intents can't be broadcasted only by the system itself.

    In the attachements, you can find the library files and a working sample of BroadcastReceiver.


    Comments, critiques, feedback is welcome!
    Have fun!

    Attached Files:

    Last edited: Sep 11, 2013
    KZero, Multiverse app, toby and 8 others like this.
  2. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Wow! This is really good :)
    I dont understand one thing though.
    What is the difference between these:
    Also do you know what the maximum value for priority will be? May be there is another app out there with higher priority than ours, so we will not be able to cancel that broadcast?
  3. XverhelstX

    XverhelstX Well-Known Member Licensed User

    Actually, it's the same. ;)
    but instead of remembering "android.provider.Telephony.SMS_RECEIVED", you could just use this Broadcast.SMS_RECEIVED.
    I just tried to add 1 more action to see if it works with more filters. (ofcourse i didn't used the same string.

    Everywhere I did a search, I came out on the maximum of 2147483647. (probably the maximum value of an integer.) So I guess most SMS applications uses this, however Android Docs state that the best value is between 999 and -999.


    Last edited: Nov 9, 2011
    toby likes this.
  4. thedesolatesoul

    thedesolatesoul Expert Licensed User

  5. JonPM

    JonPM Well-Known Member Licensed User

    Awesome! So do you know what would happen if two apps have the same priority (say 999)?

    Sent from my DROIDX
  6. Rui

    Rui Member Licensed User

    This is great:sign0060:

    Can I create my own intents and use this to broadcast messages between my applications?
    Or maybe intercept an intent and show my own dialer?

  7. XverhelstX

    XverhelstX Well-Known Member Licensed User

  8. XverhelstX

    XverhelstX Well-Known Member Licensed User

    @Rui, sure you can.

    Register a receiver in one app that listens to "info", for example.

    and one app that sends the broadcast with:

    I will still look for a way to add additional data to the Broadcast (if that is possible)

    And yes, you will normally be able to answer a call with intent: "android.intent.action.ANSWER".
    When someone calls, "android.intent.action.ANSWER" will be called. so you can intercept this, abort it and use your own activity to handle the call.

    Last edited: Nov 9, 2011
  9. myriaddev

    myriaddev Active Member Licensed User

    Wow! Thanks!

    Now I can finish a project I started some time ago!
    Thanks!, Jerry
  10. Rui

    Rui Member Licensed User

    This opens up some possibilities.

    I can't find SMS_RECEIVED in the link you posted, is this undocumented?

    Thank you.
  11. bluejay

    bluejay Active Member Licensed User

    This looks like a very useful library:)

    Thanks for making this, and thanks also for the detailed description on how to use it.

  12. sergiokv

    sergiokv Member Licensed User

    Does anyone know what's the syntax to receive broadcasts when a sms message is sent?

    I tried the following on my code and didn't work (it didn't recognize the “Broadcast.SMS_SENT” code):


    Any help or advice would be highly appreciated.

  13. JohnK

    JohnK Active Member Licensed User

    THANKS! I am using it in my Widget to be notified on a screen rotation. "android.intent.action.CONFIGURATION_CHANGED"
  14. Fabrice La

    Fabrice La Active Member Licensed User


    I am using the example from the first post.

    My SMS app in my phone still receive the SMS and after Handcent SMS.

    Don't know, but in the 1st post :
    Don' understand ????:eek:
  15. Erel

    Erel Administrator Staff Member Licensed User

  16. Fabrice La

    Fabrice La Active Member Licensed User

    I saw this post.

    My app :
    My app return True but still handcent take the sms
  17. Erel

    Erel Administrator Staff Member Licensed User

    Which priority did you use?

    You should have started a new thread for this question as it is not related to this library.
  18. agrillo

    agrillo New Member Licensed User

    Great work thomas, but how to get "extras" from broadcasted intent ?
  19. bluedude

    bluedude Well-Known Member Licensed User

    How to retreive information from the boradcasted intent?


    I think something is missing, how do we extra information from the broadcasted intent?

    Trying to use it for bluetooth signal strength but I have no clue how to extract information.

  20. salmander

    salmander Active Member Licensed User

    Hi, a question after running your sample app, I looked at the manifest file but couldn't find any
        <action android:value=
    "android.provider.Telephony.SMS_RECEIVED" />
    As far as I understand, this is necessary to receive android broadcasts. Otherwise, the app and the service needs to be running in order to receive the broadcasts. The app will not receive the broadcasts, if the OS kills the app or service.
    Am I right?
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice