Intent and Intent Filter
What is Intent?
Intent is an abstract description of operation to be performed in other terms,a Messaging object that is used to request an action from another app component.
What are the usages of Intent?
Intent can be used in following cases:
- Start an Activity
- Start Service
- Delivering Broadcast
- Sending the User to Another App
- Sending a Result from to Activity
- Sending Simple Data to Other Apps
What are the types of Intent?
There are two types of Intent: Implicit and Explicit
What is Implicit Intent?
Implicit intents 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, Capturing Image through camera.
What are the usages of Implicit Intent?
Implicit intent can be used to (Ref: Common Intents)
- Create Alarm - ACTION_SET_ALARM
- Create a timer - ACTION_SET_TIMER
- Show Alarm - ACTION_SHOW_ALARMS
- Add a calendar event - ACTION_INSERT
- Capture a picture or video ACTION_IMAGE_CAPTURE orACTION_VIDEO_CAPTURE
- Compose an email with optional attachments ACTION_SENDTO (for no attachment) or ACTION_SEND (for one attachment) or ACTION_SEND_MULTIPLE (for multiple attachments)
- Retrieve a specific type of file - ACTION_GET_CONTENT
- Open or create a specific type of file - ACTION_OPEN_DOCUMENT or ACTION_CREATE_DOCUMENT
- Show a location on a map - ACTION_VIEW
- Play a media file - ACTION_VIEW
- Initiate a phone call -ACTION_DIAL - Opens the dialer or phone app. ACTION_CALL - Places a phone call (requires the CALL_PHONE permission)
- Open a specific section of Settings - ACTION_SETTINGS
- Compose an SMS/MMS message with attachment- ACTION_SENDTO orACTION_SEND orACTION_SEND_MULTIPLE
- Load a web URL - ACTION_VIEW
- Sharing data - ACTION_SEND
What is the Diff between ACTION_GET_CONTENT and ACTION_OPEN_DOCUMNET?
As per the documentation,
ACTION_GET_CONTENT is used to get a document and copy it in your app.
ACTION_OPEN_DOCUMNET is used when you want to read an existing file without making a copy into your app, or when you want to open and edit a file in place.
Which Exception is thrown if No activity is found to handle your intent?
Whenever you invoke an intent, be ready to catch an** ActivityNotFoundException**, which occurs if there's no other activity that can handle your app's intent.
How to forcefully show a chooser for Implicit Intent?
To show the chooser, create an **Intent **using createChooser() and pass it to startActivity().
- Intent chooser = Intent.createChooser(myIntent, title);
What is Explicit Intent?
Explicit intent is used to start a component in your own app, by supplying the class name of the activity or service you want to start. For example, you might start a new activity within your app in response to a user action, or start a service to download a file in the background.
What are the usages of Explicit Intent?
- Start an Activity
- Start Service
- Delivering Broadcast
What are the Building blocks of Intent?
Intent can be created using Following:
- Component name
- Action
- Data
- Type
- Extras
- Flag
What is the component name in Intent?
It categories whether the Intent is Implicit or Explicit. It's the critical piece of information that makes an intent explicit, meaning that the intent should be delivered only to the app component defined by the component name. Without a component name, the intent is implicit.
You can set the component name with setComponent(), setClass(), setClassName(), or with the Intent constructor.
Why should we Always specify the Component name to start service?
When starting a Service, always specify the component name. Otherwise, you cannot be certain what service will respond to the intent, and the user cannot see which service starts.
Hence, Do not start service using Implicit Intent.
What is Action in Intent?
A string that specifies the generic action to perform (such as view or send, ACTION_VIEW or ACTION_SEND). It is mostly used with Implicit Intent.You can also specify your custom Action.
You can specify the action for an intent with setAction() or with an Intent constructor.
How is Action used in Broadcast sending and receiving?
In case of Broadcast action plays the main role, Actions are being performed while sending and reported while receiving. It can be systems action or customs actions too.
What is data in Intent?
The URI (a Uri object) that references the data to be acted on and/or the MIME type of that data. The type of data supplied is generally dictated by the intent's action. For example, if the action is ACTION_EDIT, the data should contain the URI of the document to edit.
When creating an intent, it's often important to specify the type of data (its MIME type) in addition to its URI.However, the MIME type can sometimes be inferred from the URI—particularly when the data is a content: URI.
If you want to set both the URI and MIME type, don't call setData() and setType() because they each nullify the value of the other. Always use setDataAndType() to set both URI and MIME type.
What is Category in Intent?
A string containing additional information about the kind of component that should handle the intent.
What are the key properties of intent?
component name, action, data, and category represent the defining characteristics of an intent. By reading these properties, the Android system is able to resolve which app component it should start.
What are the additional properties of intent that do not affect how it is resolved to an app component?
Extras and Flags do not play a role in resolving which app component it should start.
What is Extras in Intent?
Key-value pairs that carry additional information required to accomplish the requested action.
You can add extra data with various putExtra() methods, each accepting two parameters: the key name and the value. You can also create a Bundle object with all the extra data, then insert the Bundle in the Intent with putExtras().
What Kind of data Should be avoided in Extras?
Do not use Parcelable or Serializable data when sending an intent that you expect another app to receive. If an app attempts to access data in a Bundle object but does not have access to the parceled or serialized class, the system raises a RuntimeException.
What is Flag in Intent?
Flags are defined in the Intent class that function as metadata for the intent. The flags may instruct the Android system how to launch an activity (for example, which task the activity should belong to) and how to treat it after it's launched (for example, whether it belongs in the list of recent activities).
What is the difference between addFlag vs SetFlag?
When you use setFlags you are replacing the old flags. when you use addFlags you are appending new flags.
What are the Flags used to Launch Activity / what are Activity launch Modes, List Them?
Launch mode is an Android OS command that determines how the activity should be started. It specifies how every new action should be linked to the existing task.
- FLAG_ACTIVITY_NEW_TASK
- FLAG_ACTIVITY_CLEAR_TASK
- FLAG_ACTIVITY_SINGLE_TOP
- FLAG_ACTIVITY_CLEAR_TOP
How Activity, Service or Broadcast is started?
Activity can be started using startActivity() or startActivityForResult()
Service can be started using startService()
Broadcast can be sent using sendBroadcast()
What is the difference between start?
if the intent that started your activity might expect a result. If the originating activity has been called[ startActivityForResult()](https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent, int)), then the system delivers the result otherwise, the result is ignored.
How to enable your app to receive data from another application?
Application can receive data using Implicit Intent
To advertise which implicit intents your app can receive, declare one or more intent filters for each of your app components with an <intent-filter> element in your manifest file. Each intent filter specifies the type of intents it accepts based on the intent's action, data, and category.
The system delivers an implicit intent to your app component only if the intent can pass through one of your intent filters.
What is Intent Filter?
Intent Filter is declaration in manifest file about your activity or service that which Intents it can handle or receive
Intent is received only if passes through the intent filter
What are the main elements used to declare Intent Filter?
Intent filter is declared in Manifest using Action, data and category tags.
Inside the <intent-filter>, you can specify the type of intents to accept using one or more of these three elements:
Declares the intent action accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.
Declares the type of data accepted, using one or more attributes that specify various aspects of the data URI (scheme, host, port, path) and MIME type.
Declares the intent category accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.
Can Activity receive multiple types of intents? Or can we declare multiple Intent Filters for Single Activity?
Yes.
You can create a filter that includes more than one instance of <action>, <data>, or <category>. If you do, you need to be certain that the component can handle any and all combinations of those filter elements.
When you want to handle multiple kinds of intents, but only in specific combinations of action, data, and category type, then you need to create multiple intent filters.
How can we expose app Activity to other applications?
explicitly set a value for android:exported. This attribute indicates whether the app component is accessible to other apps.
If an activity, service, or broadcast receiver in your app uses intent filters and doesn't explicitly set the value for android:exported, your app can't be installed on a device that runs Android 12 or higher.
How to declare Intent Filter Dynamically?
Filters for broadcast receivers can be registered dynamically by calling[ registerReceiver()](https://developer.android.com/reference/android/content/Context#registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter, java.lang.String, android.os.Handler)). You can then unregister the receiver with unregisterReceiver(). Doing so allows your app to listen for specific broadcasts during only a specified period of time while your app is running.
How is Intent Resolution done?
For Action:
- An intent filter can declare zero or more <action> elements
- The action specified in the Intent must match one of the actions listed in the filter.
- If the filter does not list any actions, there is nothing for an intent to match, so all intents fail the test.
- if an Intent does not specify an action, it passes the test as long as the filter contains at least one action.
For category:
- An intent filter can declare zero or more <category> elements
- What Intent have must be in Intent Filter , But what IntentFilter have should not be Mandatorily in Intent
- Every category in the Intent must match a category in the filter
- The intent filter may declare more categories than are specified in the Intent and the Intent still passes
- An intent with no categories always passes this test, regardless of what categories are declared in the filter.
For data:
- TODO
How can any Activity be started by another App using Implicit Intent?
Any Activity that has declared Intent Filters can be started using its matching Intent filter by any other app if it is exported.
How can any Component be started by another App using Explicit Intent?
Before android 13:
Any Activity that is exported can be started using its component Name by any other app.it does not have anything to do with Intent Filters.
On and After android 13: (Ref :IntentFilter above 13)
If Component contains Intent Filter in its manifest declaration then to start it by another app it should also match its intent filter apart from Component name in Intent definition.
It Helps in avoiding vulnerability of components.
It should be exported in both cases above.
So regardless of intent type, if you are targeting a component from another app or any other app targeting your apps component it should always match the Intent Filter if it has been declared in android 13 and above version.
How is Intent Filter used to define Apps entry point?
app's main entry point—the activity that opens when the user initially launches the app with the launcher icon.
First of all set android:exported flag true, then Action and category as follow:
- The ACTION_MAIN action indicates this is the main entry point and does not expect any intent data.
- The CATEGORY_LAUNCHER category indicates that this activity's icon should be placed in the system's app launcher. If the <activity> element does not specify an icon with icon, then the system uses the icon from the <application> element.
These two must be paired together in order for the activity to appear in the app launcher.
Describe how broadcasts and intents work to be able to pass messages around your app?
To send
Intent i = new Intent("com.mycompany.myapp.SOME_MESSAGE");
sendBroadcast(i);
To Receive
registerReceiver(myReceiver, new IntentFilter("com.mycompany.myapp.SOME_MESSAGE"));
Why should the category be DEFAULT to receive implicit Intent?
To receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and[ startActivityForResult()](https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent, int)) treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare this category in your intent filter, no implicit intents will resolve to your activity.
How intent is used to return a result?
If you want to return a result to the activity that invoked yours, simply call[ setResult()](https://developer.android.com/reference/android/app/Activity#setResult(int, android.content.Intent)) to specify the result code and result Intent. When your operation is done and the user should return to the original activity, call finish() to close (and destroy) your activity
You must always specify a result code with the result. Generally, it's either RESULT_OK or RESULT_CANCELED. You can then provide additional data with an Intent, as necessary.
The result is set to RESULT_CANCELED by default
How to handle Incoming data from Intent?
Application receives data in Two ways: First when our Intent Filter matches with broadcast and other when we perform any action through Intent to get data(ex. Capture Photo).
In the first case we receive data in onCreate() and in the other we get data in OnActivityResult() methods.
In Both cases by Using Intent , you can get type, action and data to perform any operation.
(Ref: Receive Data)
How can two distinct Android apps interact?
Android applications can use an Intent to perform some basic interactions with other apps, such as start another app, receive a result from that app, and make your app able to respond to intents from other apps.
(Ref: Interacting with Other Apps)
What is Pending Intent?
PendingIntent is an Intent wrapped with some additional information of action to perform action.
PendingIntent is a token that an app gives to another application to perform an action on behalf. It Also allows other applications to use host apps permission to perform particular pieces of code.
(Ref: https://medium.com/androiddevelopers/all-about-pendingintents-748c8eb8619 )
What are the use cases of Pending Intent?
- Declaring an intent to be executed when the user performs an action with your Notification (the Android system's NotificationManager executes the Intent).
- Declaring an intent to be executed at a specified future time (the Android system's AlarmManager executes the Intent).
What are the components needed to create a Pending Intent?
Method takes the current app Context, the Intent you want to wrap, and one or more flags that specify how the intent should be used (such as whether the intent can be used more than once).
[PendingIntent.getActivity()](https://developer.android.com/reference/android/app/PendingIntent#getActivity(android.content.Context, int, android.content.Intent, int))for an Intent that starts an Activity.[PendingIntent.getService()](https://developer.android.com/reference/android/app/PendingIntent#getService(android.content.Context, int, android.content.Intent, int))for an Intent that starts a Service.[PendingIntent.getBroadcast()](https://developer.android.com/reference/android/app/PendingIntent#getBroadcast(android.content.Context, int, android.content.Intent, int))for an Intent that starts a BroadcastReceiver.
Intent Must be an explicit type to perform action within your app.
What is PendingIntent Mutability?
If your app targets Android 12 or higher, you must specify the mutability of each PendingIntent object that your app creates. To declare that a given PendingIntent object is mutable or immutable, use the PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_IMMUTABLE flag, respectively.
**What is FLAG_IMMUTABLE in Pending Intent?
FLAG_IMMUTABLE: Indicates the Intent inside the PendingIntent cannot be modified by other apps
**What is FLAG_MUTABLE in Pending Intent?
FLAG_MUTABLE: Indicates the Intent inside the PendingIntent should allow its contents to be updated by an app by merging values from the intent parameter of PendingIntent.send().
**What is FLAG_UPDATE_CURRENT in Pending Intent?
FLAG_UPDATE_CURRENT: Requests that the system update the existing PendingIntent with the new extra data, rather than storing a new PendingIntent. If the PendingIntent isn’t registered, then this one will be.
If an application wants to update its own Pending intent then this flag is used.
How to prevent Pending Intent from being vulnerable?
We also talked about how a PendingIntents should usually be immutable and that doing so prevents other apps from modifying it and doesn’t prevent the app from updating its own PendingIntent objects by using the FLAG_UPDATE_CURRENT flag in addition to FLAG_IMMUTABLE.
What is package visibility?
Your Application can get information about other applications installed on the device using Package manager class.But it was possible till android version 11, When you target android version 11 or above OS by default filters the visibility of other applications.
It was made a little harder process to retrieve this information by adding a <queries> entry to AndroidManifest.xml
You can do this by specifying the other apps by package name, by intent signature in <queries> tag.
Why is resolveActivity() used?
This is used to check if There is any Activity that can handle this Inten. By doing this we can avoid ActivityNotFoundException to be thrown.
So basically it is asking the OS to find the app to handle this Intent. It can be useful in cases like when some action needs to be disabled if in advance if there is no activity that can handle it
Why should we avoid resolveActivity()?
(Ref: https://cketti.de/2020/09/03/avoid-intent-resolveactivity/)
After android version 11, Package visibility is restricted and hence using this method might return a null even though using startActivity() with the intent would work just fine.To make this work you could add a <queries> entry to AndroidManifest.xml
In the past, using one method over the other was mostly a matter of taste. With the changes in Android 11 this method has the big advantage that you don’t have to add anything to AndroidManifest.xml