Skip to main content

Android Training Classes | Broadcast Receivers

Android is an open source and Linux-based operating system for mobile devices such as smartphones and tablet computers. Android was developed by the Open Handset Alliance, led by Google, and other companies. This tutorial will teach you basic Android programming and will also take you through some advance concepts related to Android application development. Learn more concepts from Android training by Learn IT Training.
Broadcast Receivers simply respond to broadcast messages from other applications or from the system itself. These messages are sometime called events or intents. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action.
Android technology is an aggregate of technologies like JAVA, C, C++ and XML, and inherits the characteristics of all these technologies. This is why Android technology is finding high acceptance   among people as it accomplishes all the requirements of users in one application only. Android applications are mostly developed in JAVA programming (Learn JAVA training by Learn IT Training from experts) accent with the use of Android Software Development Kit.
There are following two important steps to make Broadcast Receiver works for the system broadcasted intents:
  • Creating the Broadcast Receiver
  • Registering Broadcast Receiver
There is one additional steps in case you are going to implement your custom intents then you will have to create and broadcast those intents.
Creating the broadcast receiver
A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as a Intent object parameter.
Public Class  MyReceiver extends BroadcastReceiver {
@Override
Public Void onReceive(Context, “Intent Detected.”, Toast.LENGTH_LONG) .show();
}
}
Registering broadcast receiver
An application listens for specific broadcast intents by registering a broadcast receiver in AndroidManifest.xml file. Consider we are going to register MyReceiver for system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot process.
Now whenever your Android device gets booted, it will be intercepted by BroadcastReceiver MyReceiver and implemented logic inside onReceive() will be executed.
There are several system generated events defined as final static fields in the Intent class. The following table lists a few important system events.

Broadcasting custom intents
If you want your application itself should generate and send custom intents then you will have to create and send those intents by using the sendBroadcast() method inside your activity class. If you use 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.tutorialspoint.CUSTOM_INTENT”);
sendBroadcast(intent);
}
Want to learn Android and become successful Android developer,
Learn IT Training provides you with everything you need to know to become an Android developer. With its structured strategies and teaching abilities, it is sure a great place to start and build your career in android technology. It is one stop destination for all IT course and SAP training courses in SAP technical and functional modules providing trainings in India, USA, UK, Canada, Australia, New Zealand, Saudi Arabia and many countries all over the world.
Want a free demo on Android training CLICK HERE for live class.
For more details visit:
Email: contact@learnittraining.com
Phone: INDIA +91 9912989639, USA +12108585008

Comments