A intent is an abstract description of an action to be performed. Also known as a messaging component that is used to request certain action from another component.
Intents have basic fundamental use cases:
- To start an activity: Used to start new instance of an activity by passing intent to it.
ie. startActivity(new Intent(<CONTEXT_PACKAGE>, <CLASS>)).
Similarly, if we want to receive a result from the starting activity then we use startActivityForResult(<INTENT>, <RESULT_CODE>).
- To start a service: Start a service to perform one time service by passing intent to startService(<INTENT>).
- To deliver a broadcast: Deliver broadcast to other apps by passing intent on functions such as sendBroadcast(<INTENT>). There are other ways to send broadcasts as well which will help differentiate what type of broadcast is being delivered. Learn more here.
There are two types of intents
- Explicit Intent: We use explicit intents when we can specify the component to start the intent explicitly. We usually used explicit intent within our app. Typically, when you open an activity or start a service by typing the name of the component (class) itself then it is called explicit intent.
- Implicit Intent: When we don’t declare explicit component and instead specify action to be performed, which allows component from other apps to handle it.
Whenever an implicit intent is used the android system is notified to show all the available components in our mobile that can handle that action. Eg: send mail, make call etcs
A string that specifies the generic action that is to be performed.
eg: ACTION_DIAL : Dial a number,
ACTION_MAIN: Start an activity as an initial activity
We can specify our own actions for use by intents within your app.
- Data and/or Type
The URI that references the data that is to be acted on and/or also defines the MIME type of the data. setData() ,setType() or setDataAndType().
Note : If you want to set both the URI and MIME type, do not call setData() and setType() because they each nullify the value of the other. Always use setDataAndType() to set both URI and MIME type.
Additional information of the component that can handle the intent.
Key-Value pairs that contains additional information that is to be passed required to accomplish the requested action. According to the need we can add extras that can be String, boolean, integer, array list etcs.
Component is the class that should receive this intent. Use this when there is exactly one component that should be received
The flags may instruct the Android system how to launch an activity and how to treat it after it’s launched.
eg: FLAG_DEBUG_LOG_RESOLUTION: put extra debug logging information when intent is being processed.
Android filters through activities, services and broadcast receivers in the system to that handles the specified intent with the help of its fields such as action, category etcs. These intent filters are defined in manifest with tag <intent-filter>.
<activity android:name=".WebViewActivity" 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="https://techtrendzy.wordpress.com" /> </intent-filter> </activity>
Follow this link for sample code.