Course Content

Course Content

Description

Intent Filter and Broadcast Receiver in Android

Intent Filter in Android

We have seen that Intent is is 'intention to do', which means that what our activity is going to do is being decided by Intent. Now inside Intent, we use the Intent filter in Android.

Intent filters are the components that decide the behavior of intent, in our manifest file we have an activity tag, so what is going to be the work of that activity tag, we will specify it in Intent filters.

Intent filter code inside Android Manifest

<activity android:name"". MainActivity"">

<intent-filter android:icon""@drawable/icon""

android:label=""@string/label"">

<action android:name""android.intent.action.MAIN/>

<category android:name""android.intent.category.LAUNCHER""/>

</intent-filter>

</activity>

</application>

 

Intent Matching

Intents are matched against intent filters not only to discover the target component to activate but also to discover something about the set of components on the device.

Let us take a situation that we are using our phone, when we click the 'home' button we get all our application on our display, then when we click on a specific icon and the very first activity which we see in that particular application is because of ACTION_MAIN action and CATEGORY_LAUNCHER category.

 

Broadcast Receiver in Android

When we use our phone we have to deal with different types of situations, like incoming calls, our messages, our phone's battery status, etc... This all is nothing but Broadcast Receiving.

Broadcast receiving is because of two things, System generated and Application generated.

When we talk about system generated, it means it is coming from the system itself, for example 'Your phone's battery is low!' or 'Battery is fully charged.

Now when we see application-generated broadcast receiver, we see that we get different notifications from our different applications, like when we download a song.

Ways to Register Android Broadcast Receiver

Static way in which the broadcast receiver is registered in an Android application via AndroidManifest.xml file.

Another way of registering the broadcast receiver is dynamic, which is done using Context.registerReceiver() method.

When we talk about dynamic broadcasting, as it is user generated we have to also and register it otherwise it will leak. So for the unregistering dynamic broadcast receiver, we will use Context.unregisterReceiver() method.

 

Implement on Receive()

A Broadcast receiver extends broadcast receiver abstract class, which means that we have to implement the onReceive() method of this base class.

For example, if you register for ACTION_POWER_CONNECTED event then whenever power is supplied to the device your broadcast receivers onReceive() method will be invoked.

 

onReceive() Method

The onReceive() method takes two arguments.

onReceive(Context context, Intent intent)

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