In case you didn’t realise we’re changing a lot here at ruggedandmobile. Part of that change is that we’re a lot more focused on Android software and IOT, as well as providing our own range of rugged devices so we’re busy at all times working on all kinds of software and apps that we can bring to the market to enrich the lives of our customers.
We want to share our knowledge and expertise though so today we’re starting a brand new blog section, aptly called “Larry’s corner”. Who better to talk about Android, .NET and code in general than our faithful little robot Larry! Sometimes it’ll be a code snippet, sometimes some advice or sometimes a deep dive, hopefully we’ll be able to help a few people out as we blog about our coding trials and tribulations!
So today I thought we’d celebrate with a weekly series of articles and where better to start than with a deep dive on Android Broadcast receivers!
We currently creating a Battery usage manager app for our Raptor toolbelt and one on the things we need to do on this is to monitor the battery level. However the app is really only ever ran when the user wants to set a few things up, so we need to monitor the battery but without the app being run… How the heck do you do that!!
Well with Android it’s easy by using Broadcast receivers and here’s how you can do a few things with them. In this series of articles we’ll explore:
- Broadcast receivers explained (This article)
- Creating a broadcast receiver
- Using the onResume/onPause
- Boot resistant Broadcast Receivers using BOOT_ONCOMPLETE
- Using services
Before we go on, lets stop to think just what broadcast receivers actually are? Well they’re one of the core elements of any decent operating system, and come in all different guises. Microsoft .NET implements a similar system with it’s “Event handlers” and Notification brokers but Android is especially good with them. The Android OS has many different components to it, both hardware and software and some of these components are broadcasting out all kinds of information. Lets take the Battery as an example, which is constantly broadcasting out things like it’s battery level, temperature, whether its charging or dis-charging and more. In fact if you go to the Android Battery Manager dev page, you’ll see all kinds of things you can get from it. So if you want to build an app that makes use of this kind of “Broadcasted” information you need to build somehting that will capture this information, a “broadcast receiver”. Simple!
The second element you need to know about when building your first broadcast receiver is the “Intent” you will use to build it. Don’t worry about that for now, all you need to know is that an “intent” is simply telling the broadcast receiver you’re building, exactly what you want tit to listen for. It’s within the intent that you will determine if you want the battery level, or maybe something else like the camera button being pressed.
Once coded, your app will then register it’s interest and will be one of the apps that will run and then handle the intent you just asked for.
As you can see from the above list of topics we’re going to cover, there’s a lot more to broadcast receivers than meets the eye, however after this series of articles i’m hoping you’ll be an expert, able to build all kinds of programs that use many different types of broadcast receivers.
Next up we’ll be rolling up our sleeves and doing coding our first basic broadcast receiver.