Android Development

Intent Filters and Broadcast Receivers

You have perceived how an Intent has been utilized to call another movement. Android OS utilizes channels to pinpoint the arrangement of Activities, Services, and Broadcast beneficiaries that can deal with the Intent with assistance of indicated set of activity, classes, information plot related with an Intent. You will utilize <intent-filter> component in the show scrape to list down activities, classifications and information types related with any movement, administration, or broadcast receiver. 

Following is an example of a part of AndroidManifest.xml 

<activity android:name=".CustomActivity" 

   android:label="@string/app_name"> 

    

   <intent-filter> 

      <action android:name="android.intent.action.VIEW" /> 

      <action android:name="com.example.My Application.LAUNCH" /> 

      <category android:name="android.intent.category.DEFAULT" /> 

      <data android:scheme="http" /> 

   </intent-filter> 

    

</activity> 

Once this activity is defined along with above mentioned filters, other activities will be able to invoke this activity using either the android.intent.action.VIEW, or using the com.example.My Application.LAUNCH action provided their category is android.intent.category.DEFAULT. 

The <data> element specifies the data type expected by the activity to be called and for above example our custom activity expects the data to start with the "http://" 

There may be a situation that an intent can pass through the filters of more than one activity or service, the user may be asked which component to activate. An exception is raised if no target can be found. 

There are following test Android checks before invoking an activity − 

A filter <intent-filter> may list more than one action as shown above but this list cannot be empty; a filter must contain at least one <action> element, otherwise it will block all intents. If more than one actions are mentioned then Android tries to match one of the mentioned actions before invoking the activity. 

A filter <intent-filter> may list zero, one or more than one categories. if there is no category mentioned then Android always pass this test but if more than one categories are mentioned then for an intent to pass the category test, every category in the Intent object must match a category in the filter. 

Each <data> element can specify a URI and a data type (MIME media type). There are separate attributes like scheme, host, port, and path for each part of the URI. An Intent object that contains both a URI and a data type passes the data type part of the test only if its type matches a type listed in the filter. 

Android Broadcast Receiver is an Android component that is used to broadcast the messages to the system or other applications. The broadcast message is referred to as an Event or Intent. Broadcast receivers, unlike Activities, have no user interface. It’s working is similar to that of a publish-subscribe design pattern. It’s used for Asynchronous Inter-Process communication. 

Intents (Prev Lesson)
(Next Lesson) Service
Back to Android Development

No Comments

Give a comment