AlignMinds Technologies logo

Broadcast Announcements & Broadcast Receiver in Android

Android Broadcast Receiver is a component that responds to the system’s wide broadcast announcements. It can be registered for various system or application events. Whenever those events occur the system notifies all the registered broadcast receivers and then the desired action is being done. Broadcast originates from the system as well as applications. Like the alarm notification, low battery notification etc. are the example of broadcast originating from the system. While getting the push notifications for desired application describes the example for broadcast originating from the application. 

How to make BroadcastReceiver works for the system broadcasted intents?

There are mainly two steps to make BroadcastReceiver works for the system broadcasted intents

  • Creating a Broadcast Receiver
  • Register Broadcast Receiver

1. Creating a Broadcast Receiver

public class MyBroadcastReceiver extends BroadcastReceiver {public MyBroadcastReceiver () { }@Overridepublic void onReceive(Context context, Intent intent) {

This method is called when this BroadcastReceiver receives an Intent broadcast.

Toast.makeText(context, “Action: ” + intent.getAction(), Toast.LENGTH_SHORT).show();}}

Consider a receiver class named as MyBroadcastReceiver implemented as the subclass of BroadcastReceiever class which overrides the onReceive() method. Whenever an event occurs Android calls the onReceive() method on all registered broadcast receivers. In the above code, the Intent object is passed with all the additional information required and also you have got the Context object in order to do other tasks like maybe start a service (context.startService(new Intent(this, TestService.class))

Register the Broadcast Receiver

There are two ways to register the broadcast receiver:

  • Static way (in manifest file).
  • Dynamic way (in code).

Static way

In static way, the broadcast receiver is registered in an android application via AndroidManifest.xml file.  Consider, here we are going to register MyBroadcstReceiver for system-generated event ACTION_BATTERY_LOW which is fired by the system once the android system encounters the battery low.

<applicationandroid:icon=”@drawable/ic_launcher”android:label=
”@string/app_name”android:theme=”@style/AppTheme”><receiverandroid:name=”MyBroadcastReceiver”><intent-filter><actionandroid:name=”android.intent.action.BATTERY_LOW”></action></intent-filter></receiver></application>

Now, whenever your android device will encounter battery low problem it will trigger the BroadcastReceiver, MyBroadcastReceiver and the desired action mentioned inside onReceive() method will be executed. Like, if you look into your android device you gets a dialogue message warning you that the battery is low so put it in charge.

Some other important system events are as follows:

Event Constant Description
android.intent.action.BATTERY_CHANGED Sticky broadcast containing the charging state, level, and other information about the battery.
android.intent.action.BATTERY_LOW Indicates low battery condition on the device.
android.intent.action.BATTERY_OKAY Indicates the battery is now okay after being low.
android.intent.action.BOOT_COMPLETED
This is broadcasted once, after the system has finished booting.
android.intent.action.BUG_REPORT Show activity for reporting a bug.
android.intent.action.CALL Perform a call to someone specified by the data.
android.intent.action.CALL_BUTTON The user pressed the “call” button to go to the dialer or other appropriate UI for placing a call.
android.intent.action.DATE_CHANGED The date has changed.
android.intent.action.REBOOT Have the device reboot.

Dynamic way

In dynamic way, we use Context.registerReceiver() method. Dynamically registered broadcast receivers can be unregistered using Context.unregisterReceiver() method.

BroadcastReceivermReceiver=newMyBroadcastReceiver();
registerReceiver(this.myReceiver,newIntentFilter(“MyBroadcast”));

IntentFilter object that specifies which event/intent our receiver will listen to. In this case, it’s broadcast. This action name is used while sending a broadcast that will be handled by this receiver.

@Overrideprotected void onPause() {unregisterReceiver(mReceiver);super.onPause(); }

Once the component that had made the registerReceiver() call is destroyed sendBroadcast() will also stop working, hence the receiver won’t receive anymore be it an event generated from an app or the system. Whereas with the previous method where we registered via the manifest file, this is not the case.

Dynamically registered receivers are called on the UI thread. Dynamically registered receivers block any UI handling and thus the onReceive() method should be as fast as possible. The application may become sluggish of an “Application Not Responding” error is the worst.

Which Method to Use When for Registration

The type of preference among the two approaches is determined by the motive. Suppose you want to do some changes right on the screen (home screen, launcher, status bar, etc.) By showing up some notification or some indicator in the status bar by listening to system-wide events or maybe those sent by other apps, then it makes sense to use statically registered broadcast receivers. Whereas based on similar events you want to do changes right in your app when the user is using it or maybe it’s put in the background, then it makes sense to use dynamically registered receivers which last till the registering components are destroyed.

In fact, there are certain events like Intent.ACTION_TIME_TICK that cannot be registered in the manifest but only via registerReceiver() to prevent battery drainage.

Broadcasting Custom Intents

If one wants that the application itself should generate and send custom intents then one will have to create and send those intents by using the sendBroadcast() method inside the activity class. If one uses the sendStickyBroadcast(Intent) method, the Intent is sticky, meaning the Intent you are sending stays around after the broadcast is complete.

public void broadcastIntent(View view){Intent intent = new Intent();intent.setAction(“com.example.broadcastreceiverdemo“);
sendBroadcast(intent);}

This intent com.example.broadcastreceiverdemo can also be registered in a similar way as we have registered system-generated intent.

<applicationandroid:icon=”@drawable/ic_launcher”android:label=
”@string/app_name”android:theme=”@style/AppTheme”><receiver android:name=”MyReceiver”><intent-filter><action android:name=” com.example.broadcastreceiverdemo“></action></intent-filter></receiver></application>

You can see a working example by visiting this link.

– Deepika Bisht

Android: World’s Most Popular Mobile Platform – But why?

Android is the most used Mobile OS and it has gained so much popularity today that there is no single place where Android is not used. Especially popular with Smartphones, Android supports most of the applications in smartphones without the support of any third-party applications.

Life has become much easier and advanced through these applications. Now, you can get everything (weather updates, latest news, sending e-mails, messaging, video chatting, conference calls, downloads, navigation, deals, nearby locations, entertainment and what not!) at a single place and within your comfort zone.

Why Android became so popular?

Android (owned by Google) is a Linux based Operating System designed for touch screen mobile phones. There are so many Android devices available in the market today. Companies like Samsung, HTC, and Motorola are few of the major competitors that make (or made) use of Android platform in their devices. Ability to customize the code (Android is based on the open-source platform) is the major advantage of Android that makes it outstanding when compared to other OSs.

Android phones give a real-life experience to the end-user. Faster multitasking, better system performance by optimizing memory, improved touchscreen, fresh design, quick response and ease of use are some of the features that gained Android its popularity.

The Adoption rate of Android into Smartphones and other devices

Android is the most used mobile OS today. As of July 2013, around 1 million Android Apps were published in Google Play store and there were around 50 billion downloads of only Android Apps. Selling figures indicate that Android devices were mostly sold in 2012, 2013, and 2014 than any other mobile devices.

Google revealed that there were around 540 million active users (who are using the Android mobile for the last 30 days) in 2013 which rose to almost 1 billion by 2014. A developer survey conducted in May 2013 indicates that 75% of mobile developers developed using Android OS.

By December 2019, the market share of Android OS stands at 74% of the total market. Android’s competitor, iOS stand at the second position in the list with a market share of 24%.

Among various Android versions, Android 9.0 Pie has the most market share with a percentage of 41.86 (January 2020). 8.1 Oreo stands in the second position.

Reference: Statista

Reference: Statcounter.com

The Open Source Linux Community let developers build powerful and innovative applications using the latest mobile technologies.

Android is not only widely used in field of smartphones, it has also gained popularity in various other areas like Gadgets (example, wristwatch that incorporates MP3 player, GPS tracker, breath and heartbeat rate), Home Appliances (example, television, refrigerators, washing machine etc), and Automobiles (example, carmakers have designed Android-based Infotainment systems that have voice-controlled navigation system, browser and an app store)

The evolution of Android

Google always tried to introduce more powerful and user-friendly features in every release of Android. New Interface, better runtime for apps and more security are some of its major features that always got un upgradation during every new release.

Better Visual Appearance

The visual appearance of Android has become more colourful and animated throughout the past years. The transition between screens, clicks, and navigation buttons have become more user-friendly.

Improved Notifications

The overlay notification has helped users to keep an eye on what’s new on their devices. It not only helps them to read new messages and call details but also easily access notification from the application of their choice. Due to the expansion of Android OS into other devices like a smartwatch, now notifications can be read just by looking at a watch on your wrist.

Battery Improvement

Battery life has much improved in every version of Android. Battery drain can be tracked with the battery saver mode and non-essential services can be turned off or run in set intervals for extended battery life.

Monitoring Health

Google’s framework “Google Fit” lets you keep track of your physical activity and food intake and give you alert regarding heart rate, breathing etc.

Security

Unlocking your device in a secured environment is faster now. A feature called “personal unlocking” unlocks your phone automatically if you are in a pre-programmed location or if it recognizes your voice.

Google Assistant

With the introduction of Google Assistant, Android smartphones have become more popular among users. Google assistant enable users to ‘command’ their smartphones without even touching it. The interactive workflow adopted by this virtual assistant made it superior to any other assistant, for example like Siri, which is less interactive in comparison.

Android in the last 10 years

Here are 4 powerful features that kept Android on the top list among competitors in the past 10 years.

Messaging

Messaging has become even more friendly by unifying different features like emailing, SMS, instant messaging, video calling into a single platform. The transition between different methods of calling would be so smooth without having the hassles to handle different apps for each feature.

Google Maps

The latest version of Google Maps provides a greater level of customization based on input provided and recent searches made. Google Now and Google Latitude track your every step, you will find your favourite fun spot every weekend (based on your previous visits tracked by the app). With handy features, with few layers of polishing though, it will still be the best free solution available on mobiles.

Payments and Security

Payments, money transfer etc. has become easier in the last few years. The Google Authenticator communicates with your device’s NFC Chip to make automatic payments at your favourite restaurant or log you in to your Facebook account when you sit down at your laptop or even verify your identity using basic face recognition combined by fingerprint or retina scanning built into your device’s camera app.

Hardware

Optimized displays, extended battery life, advanced mobile processors, better security are few of the major hardware innovations that played a big part in making Android the popular OS.

With the invent Google Assistant, you can just speak to make a call using voice recognition. It also helps in automatically finding the contact number and make the desired call or you can just mention to order some food from your favourite restaurant at a set time and it will take care of placing the order and making payments.

Not just that, you do not even need to check what you want to eat today as Google will track your entire day’s events and what you have been eating all day and based on this, it will suggest a menu. You don’t even need to take pictures, as Google will simply pick the best pictures based on its collection of your entire day’s activities.

Conclusion

With all the new exciting features and enhancements coming up, Android is ready to take things to a whole new level making the world even smaller and more comfortable place to live.

– Susan B. John