Intents in Android
The official Documentation defines intents as messaging objects that we can use to request an action from another app component. Intents facilitate communication between components like sending data between activities and so on.
We can use intents to
Start an activity; here we create an intent object passing the activity name and the necessary data. We then start a new instance of an Activity by passing an Intent to startActivity().
Sending Broadcasts; A broadcast is a message that any app on you can receive. The system delivers various broadcasts for system events like plugin in your earphones.
The created intent object here is passed to sendBroadcast() or sendOrderedBroadcast().
Start a service; Services are background tasks that run without providing an interface like activities.As of android 5.0 (lollipop)We use the JobScheduler to start services. Anything earlier than that uses methods from the service class.
Here you would also need to create an intent object with the necessary data and pass it to
startService().
Types of Intents
There are two types of intents
Explicit intents: We specify which application or application component will satisfy the intent, by supplying either the target app's package name or a fully-qualified component class name. We use Explicit intents to start Activities or Services since we know the activity or service we want to start.
Implicit intents: here we do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, opening a PDF document on our phones. The Android system searches all apps till it finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object. If multiple intent filters are compatible, the system displays a dialog so the user can pick which app to use.
INTENT FILTERS; these are expression in an app's manifest file that specifies the type of intents that the app can to receive.
Structure of intents
Building intents requires us to declare certain properties, by using these properties declared, the Android system is able to resolve which app component it should start.
A component name; declaring this makes an intent explicit as we name which app component that will satisfy the intent.
Data: This is the resource the intent operates on. It is expressed as a Uniform Resource Identifier or Uri object in Android. URI’s are unique identifier for a particular resource. An intent may require a specific type of data depending on the action. We would need an image for an ACTION_VIEW, or a phone number for an ACTION_DIAL.
Category: This string contains information about the kind of component that should handle the intent. However, most intents don’t need us to specify this. For example, CATEGORY_APP_MUSIC which is Used with ACTION_MAIN to launch the music application.
An Action; this is a string like specification of the action to perform. We can specify our own actions for use by intents within your app but there are specific action constants defined by the Intent class we use. For example
ACTION_VIEW : we can use this to view a map or image in another app or app component
Intents also use Extras and flags which are additional information that won’t affect the way intents are resolved to an app component. Extras are key-value pairs with additional information required to accomplish the requested action. Certain actions use specific Extras.
Pending Intents
Pending Intent object act as wrappers for the Intent objects you have created. PendingIntents allow a foreign application to use the contained Intent object as if it were executed from your application’s own process.We use PendingIntents in various ways;
Declaring an intent to be executed at a specified future time.
Declaring an intent to be used when the user performs an action with an App Widget.
Declaring an intent to be executed when the user performs an action with your Notification
Comments
Post a Comment