Android Intents & IntentFilters

What is Android Intents & IntentFilters

An Intentis a declaration of need.
•An Intentis made up of various pieces including:
–desired actionor service,
–data, and
–categoryof component that should handle the intent and instructions on how to launch a target activity.
•An IntentFilteris a trigger, a declaration of capability and interest in offering assistance to those in need.
•An IntentFiltermay be generic or specific with respect to which Intents it offers to service.
An intentis an abstract description of an operation to be performed.
•Its most significant use is in the launching of activities, where it can be thought of as the glue between activities.
•The primary pieces of information in an intent are:



Android intents and intent filters
Some examples of Intent’s action/data pairs are:
ACTION_VIEWcontent://contacts/1--Display information about the person whose identifier is "1".
ACTION_DIALcontent://contacts/1--Display the phone dialer with the person filled in.
ACTION_VIEWtel:123--Display the phone dialer with the given number filled in
ACTION_DIAL tel:123--Display the phone dialer with the given number filled in.
ACTION_EDITcontent://contacts/1--Edit information about the person whose identifier is "1".
ACTION_VIEWcontent://contacts/--Display a list of people, which the user can browse through.



Dissecting Intents

1.Component nameThe name of the component that should handle the intent ( for example "com.example.project.app.MyActivity1" ).
 
2.ActionA string naming the action to be performed —or, in the case of broadcast intents, the action that took place and is being reported (for example: ACTION_VIEW, ACTION_CALL, ACTION_TIMEZONE_CHANGED, … ).
 
3.DataThe URI of the data to be acted on and the MIME type of that data (for example tel:/216 555-1234 , "http://maps.google.com”, ... ).
 
4.CategoryA string containing additional information about the kind of component that should handle the intent (for example CATEGORY_BROWSABLE, CATEGORY_LAUNCHER, … ).
 
5.ExtrasKey-value pairsfor additional information that should be delivered to the component handling the intent.
 
6.Flagsof various sorts.

Delivering Intents 

An Intent object is passed to
Context.startActivity()or Activity.startActivityForResult()
to launch an activity or get an existing activity to do something new (asynchronous& synchronously respectively).

•An Intent object is passed to Context.startService()to initiate a service or deliver new instructions to an ongoing service.

•An intent can be passed to Context.bindService()to establish a connection between the calling component and a target service. It can optionally initiate the service if it's not already running.

Intent Resolution 

Intents can be divided into two groups:
•Explicit intentsdesignate the target component by its name, typically used for an activity starting a subordinate service or launching a sister activity.
•Implicit intentsdo not name a target (the field for the component name is blank). Implicit intents are often used to activate components in other applications. Late binding applies.
Whenever possible Android delivers an explicit intent to an instance of the designated target class.

Example of Intent  

Following fragments calls an Intentwhose job is to invoke a built-in task (ACTION_VIEW) and explore the Contactsavailable in the phone.
[Intent myIntent= new Intent(
Intent.ACTION_VIEW,
Uri.parse("content://contacts/people"));
startActivity(myIntent);35] 

code example

package matos.cis493;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class AndDemo1 extends Activity {
/** show contact list */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent myIntent= new Intent(Intent.ACTION_VIEW,Uri.parse("content://contacts/people"));
startActivity(myIntent);
}

 

 

IntentFilters 

The IntentFilterdefines the relationship between the Intent and the application.
•IntentFilterscan be specific to the data portion of the Intent, the action portion, or both.
•IntentFiltersalso contain a field known as a category. A category helps classify the action.
•For example, the category named
CATEGORY_LAUNCHER
instructs Android that the Activity containing this IntentFiltershould be visible in the home screen.
 When an Intentis dispatched, the system evaluates the available Activities, Services, and registered BroadcastReceiversand routes the Intent to the most appropriaterecipient (see next Figure).
 

IntentFilters 

To inform the system which implicitintents they can handle, activities, services, and broadcast receiverscan have one or more intent filters.
•Each filter describes a capability that the component is willing to receive.
•An explicitintent is always delivered to its target, no matter what it contains; the filter is not consulted.
•But an implicit intent is delivered to a component only if it can pass through one of the component's filters.
 

0 comments: