Intents in Android
Did you ever see how a new android Activity opens when you click on a button, suppose the settings button to show the settings screen in any app? How does the app open up when we click on the notification? How do you get Low battery alert in our mobile? All these screens are possible because of Intent in Android.
The Intent is a messaging thing that we can use to request an action from an app component. The Intent is basically an interaction to do an action and it’s a way to communicate between Android screen to request an action from a component, by different components.
It’s like sending a message that Android listens for and then it affected accordingly by identifying and invoking the app’s appropriate component and It can be within that same app or some other app as well.
If multiple android apps are capable of responding to the message then Android provides the user with a list of those apps from which a choice can be made.
Uses of Intent in Android
There are three fundamental uses of intents:
- To start an Activity
The android Activity represents a single screen in an app and we can start a new instance of an Activity by passing an Intent to startActivity() and android Intent describes the activity to start and carries any required data along. - To start a Service
The Service is an object that performs operations in the background and does not have a user interface and we can start a service to perform a one-time operation by passing an Intent in startService() and android Intent describes which service to start and carries any necessary data. - To deliver a Broadcast
The broadcast is a message that any android app can receive and the system delivers various broadcasts for system events where such as when the system boots up or the device starts charging and we can deliver a broadcast to other apps by passing an Intent to sendBroadcast() or sendOrderedBroadcast().
Types of Intents
In Android, there are two types of Intents:
- Explicit Intents
- Implicit Intents
Explicit Intents
When we explicitly define which Android objects or components should be opened on some user action and then we use explicit intents so that we generally use an explicit intent to start a new component in your own app and because we know which exact activity or service you want to start and you can start a new android activity in response back to a user action or start a service to download a file in the background.
Create an Explicit Intent
To create an explicit intent,
- We need to make an Intent object and the constructor of the Explicit Intent’s object needs two parameters as follows:
Context c:This represents the android object of the Activity from where you are calling the intent. - Call startActivity() function and pass the intent’s object as the parameter in android. This function is open to the java file mentioned in the Intent’s object.
- If we need to pass some information or data to the new Activity we are calling and we can do this by calling putExtra() function before the startActivity() method and This function requires key-value pair as its parameter.
Java file name:This shows the name of the java file of the Activity you want to open.
Intent i = new Intent(this, MyJavaFile.class);
startActivity(i);
i.putExtra("key1", "I am value1"); i.putExtra("key2", "I am value2"); startActivity(i); String a = getIntent().getStringExtra("key1");
By Doing this it stores the value stored at key1 into the string variable a.
Implicit Intent
When we just have to tell what action we want to perform without worrying which component will perform it and then you can use implicit intent.
Implicit intents do not name by the specific component to perform a particular action and instead it declares a general action to be performed, which allows any component, so even from another app to handle it.
For example, if we want to show a specific location of the user on a map, we will use an implicit intent to pass the coordinates through the intent and then any other app, so by which It is capable of showing the coordinates on a map can accept that intent.
Create an Implicit Intent
To create an implicit intent,
- You need to make an android Intent object and the constructor of the Implicit Intent’s object needs a type of action you want to perform.
An action is a string that specifies the generic action to be performed and the action largely determines how the rest of the intent is structured.
ACTION_SEND: It will be used when you have some data that the user can share through another app such as an Email app. - We need to provide some data for the action to be performed and Data is typically expressed as a URIwhich provides data to the other app so that any other app which is capable of handling the URI data can perform the required action. so if we want to open a website through your app, we can pass the URI data using setData() function as follows:
- Call startActivity() function in the end with the android intent object as the parameter.
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.google.co.in"));
startActivity(i);