Skip to content

Instantly share code, notes, and snippets.

@Aeonitis
Last active February 14, 2017 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aeonitis/1dc77edcafffff1b6e8023c1c1cc24bc to your computer and use it in GitHub Desktop.
Save Aeonitis/1dc77edcafffff1b6e8023c1c1cc24bc to your computer and use it in GitHub Desktop.
Android - Utilizing BroadcastReceiver to check for Device status updates [i.e. Location (GPS&Net)/Network (Wifi&Mobile)/Battery (Boot&Low)] in Realtime
BroadcastReceiver is an application component in which you can send broadcasts (via intents) from one component and receive it in another.
Simply put, it is a great way of preparing your app to listen out for changes to your mobile in realtime actively (by users) or passively (Operating System).
In this example we will demonstrate how to implement both a minimal and optimal implementation of the BroadcastReceiver.
This solution below checks specifically for Device Location State updates.
Minimal Solution:
Go to URL https://github.com/Aeonitis/lh-and/commit/6a4c271736a32ef107e88fd5575108787cbdf25e
or search for my commit '6a4c271736a32ef107e88fd5575108787cbdf25e' on Github, in case I changed my repo name
Optimal Solution (using interfaces):
Go to URL https://github.com/Aeonitis/lh-and/commit/8f4a6f2d90b6203a3a9a5eec10585ad089190f14
or search for my commit '8f4a6f2d90b6203a3a9a5eec10585ad089190f14' on Github, in case I changed my repo name
For receivers checking other device status updates e.g. Network State
They are received on function onReceive() & sent by sendBroadcast(Intent).
You can either:-
1. dynamically register an instance of this class with Context.registerReceiver() or
2. statically declare an implementation with the <receiver> tag in your AndroidManifest.xml.
My solutions are both dynamic
Source(s): https://developer.android.com/reference/android/content/BroadcastReceiver.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment