Intent class in android – part 2
Intent class in android
In previous article we discussed Intent class. In this article we will explore Intent Class in more detail and how we can start activities with Intents. We can start activity by calling
startActivity(Intent intent, ...)
and
startActivityForResult(Intent intent, ...)
When we call these methods, Android has to figure out which single activity it is going to call. Android will do this using two ways.
Explicit Activation of Activity
1. Target activity can be named explicitly by setting Intent’s component.
2. Android can implicitly determine based on Intent that was used and the properties of the activities that we have used in our device. Please consider the code snippet below. It is an app that have a login page and user logs in and then next activity is called that shows a welcome message to the user after successful login.
In the code above we get username and password and pass it into checkPassword function, After successful login an Intent called helloAndroidIntent is created and to its constructor we pass two parameters.
1. A context
2. A class object for the activity to be started
In this case we pass LoginScreen.this as context, A context is an interface that is used to access global application information and since activity is a subclass of context so its fine to use LoginActivity.this for this parameter and second parameter is the class object for HelloAndroid class, This parameter is sued to indicate the activity to be started by the call to startActivity
Implicit Activation of Activity
When the activity to be activated is not explicitly named, Android tries to find activities that match the Intent and tries to figure on its own.
In this case Android tries to match the Intent that was passed into the startActivity or startActivityForResult, with the capabilities of the activities that are on our device and
This Process is called Intent Resolution.
Intent Resolution Process
Intent resolution relies on two kinds of information,
1. An Intent describing the a desired operation that it want to be performed.
2. IntentFilters which describe which operations an activity can handle.
This information is usually placed in AndroidManifest.XML file for the application to which these activities belong.
At the run time when startActivity method is actually called Android will try to match the Intent wiht Intentfilters that it knows about , however it is going to use part of Intent Information when it does thsi matching, specifically it will look the
Action field
Data (Including both URI & Type)
Category
Specifying Intent Filters in AndroidManifest.XML file
Please consider the code snippet below that if an activity can dial phone numbers it should include an Intent filter with the standard filter INTENT.ACTION_DIAL (This is the Java name for this), The actual string value is “android.intent.action.dial”. That is the actual value that is value that is actually written in IntentFilter.
Adding Data to Intent Filter.
An activity can specify MimeType of the data it can handle and also it can specify the scheme, host and a port number of the data URIs it can handle. It can further limit the format of data by specifying its path, pathPattren and pathPrefix.
Receiving Implicit Intents
To receive an implicit intents, an activity should specify an IntentFilter with the category,
ANDROID.INTENT..CATEGORY.DEFAULT
Priority
Android:Priority Priority given to the parent component when handling matching Intents that causes android to prefer one activity over other. and value for this priority should be between -1000 to +1000 and higher values means higher priorities.
Investigating Intent Filters
To explore more about Intent Filters please explore and look at the command in terminal
% adb shell dumpsys package
This command will give lot of information about Intent filter.
So That’s it for this article, Please stay tuned for the next article in which we will explore Permissions in Android.