Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

How to create Intent and Broadcast Receiver component with an example in Android

 

Intent

 

The basic function of Intent is to pass messages between Android components, such as activities, content provider, services, broadcast receiver, etc ...

 

The dictionary meaning of 'Intent' is intention or purpose so it can be described as the intention to do action.

 

Android Intents are mainly used to :

  • Start the Service.
  • Display a Web Page.
  • Display a List of Contacts.
  • Dial a Phone Call etc.

As an example, we take a phone call, if I have to call someone, my most basic activity would be to open the dialer first, and then dial the phone number, as soon as I click on the call button then Intent will start calling from the particular application of calling. This means that Intent calling and Dialer communication was done here, and the task of calling has being achieved. Precisely, Dialer is the primary activity from which we will call and Calling is secondary activity.

 

Android Broadcast Receiver

 

The next component now is a Android Broadcast Receiver. Notification is a very common thing that keeps on coming in our mobile phones throughout the day, such as messages coming in, WiFi connected or disconnected, everything that happens is nothing but a broadcast receiving.

 

How Broadcast Receiver works ?

  •  Broadcast receiver is temporarily inactive component.
  • This activates when we register it for an intent.

Suppose we have created an intent in which we have written the code for wifi activation, and we have said that as soon as wifi comes our intent will run and it will tell that wifi is activated, and when I am turning off wifi immediately I get the notification that the wifi is deactivated. That is, here we have kept that Intent registered, for activation and deactivation of wifi.

 

Creating the Broadcast Receiver

 

Public class My Receiver extends Broadcast Receiver.

{

                @Override public void on Receiver(Context context, Intent intent)

                {

                                Toast.makeText(context, intent Detected.”,

                                Toast.LENGTH_LONG.show();

                }

}

 

Here at first we have an Intent registered and as soon as that event is performed we will get the notification, now for this we take example of 'Truecaller' application, which is a very common application. Suppose there is a number and I want to block it, then we will go to the Truecaller application and activate that number in the block option, that is, here we have first registered an Intent that we can say we blocked the number, now as soon as that particular call comes to my Android system, then the intent of the Truecaller that has been registered will occur, and immediately we will get a notification that we received a call from this number and that number is now blocked.

 

Now we will see the second example of broadcast receiver, such as Battery, where we have made the Intent that the notification of battery low will be immediately visible on our mobile screen as soon as the battery is low.

 

Next example, suppose I have to go to some social gathering, then in the 'Remainder' application we can register our intent and as soon as the time comes, it will give us notification of it.

 

Broadcast receivers have many of examples, such as message notifications, and lots of real-time examples.

 

System Broadcasts

 

Event

Description

Intent ACTION_BOOT_COMPLETED

Boot completed requires the Android. RECEIVE_BOOT_COMPLETED permission.

Intent ACTION_POWER_COMPLETED

Power got connected to the device.

Intent ACTION_POWER_DISCONNECTED

Power got disconnected to the device.

Intent ACTION_BATTERY_LOW

Triggered low battery, typically used to reduce activities in your app which consume power.

Intent ACTION_BATTERY_OKAY

Battery status good again.

 

So here we learn about the How to create Intent and Broadcast Receiver component and Now we take broadcast receiver example for better understanding

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere