Implicit Intent in Android Example

Even though smart phones have become inseparable companions for nearly all of us, we do not think much about the activities taking place behind everything that we see on the screen. For instance, many of us do not know that the applications on our smart phones can interact and share information with each other. This brings us to our new subject: Implicit Intent in Android.

Before we explain Implicit Intent in Android to you, you must understand what an Android Intent is. Suppose you are using a social media application, such as Instagram, and want to click a picture in the application. In this situation, an Android Intent will enable your social app to simply request the camera app to click a picture and send the picture back to the social app.

Therefore, an Android Intent is used when an application needs to make a request to any Android component of another application (or the same application) on a device in order to perform an operation. In simpler words, an Android Intent is a bridge between Android components. By using an Android Intent, app components communicate with each other, requesting for required actions.

In actuality, the object of an Android Intent is a PDS (passive data structure) that carries only abstract description of the operation that is required. In short, the object holds information that defines the required operation. The Android System, then, uses this information to select the component that can handle the operation or “intent.”

Types of Android Intent

On the basis of the targeted component of the Intent, Android Intent is divided into two types: Implicit Intent and Explicit Intent. In this tutorial, we will discuss Implicit Intent only. For Explicit Intent, a separate tutorial is available on our website.

Implicit Intent in Android

In Android development, Implicit Intent is the type of Intent that does not specify the targeted component. Instead, Implicit Intent describes the operation (and sometimes data to provide content for the operation) that is required by the app. When an Android System receives an incoming Intent, it checks the whole device for all the components that can handle the Intent. When there is only one component that is able to handle the Intent, the system simply launches that single component.

However, if the device has more than one component that can handle the Intent, then the system displays a dialog box to the user. This dialog box shows all the appropriate components for the Intent. Out of these choices, the user has to select only one to perform the required operation.

The mode of action of Implicit Intent in Android enables it to perform basic interactions with other apps.For better understanding of Implicit Intent, let us give you a few examples. In the beginning of this article, we gave you an example that described an interaction between a social app and the camera app. That was an example of Implicit Intent.

Another example of Implicit Intent is when an Android app developer wants to show a location on a map to the user. For this, the developer can use an Implicit Intent to send a request to a capable app, such as Google Maps, asking to show the specific location on its map.

Important Methods of Implicit Intent in Android

In Android development, many methods are used to implement an Implicit Intent in an application. However, there are a few fundamental methods that are so important to execute an Implicit Intent that all Android developers must learn about them. So here are the three most important methods for implementing Implicit Intent in Android.

Context.startActivity() This method is used for either launching a new activity or making an existing activity to perform the action.

Context.startService() This method is used for either starting a new service or delivering instructions to an existence service.

Context.sendBroadcast() This method is used for delivering the message to broadcast receivers.

Implicit Intent in Android Steps

After covering all the important points regarding Implicit Intent, the only thing left now is implementing Implicit Intent in Android. In this example, we will make use of Implicit Intent to open a webpage in a way that makes multiple apps, our project and a browser(s), interact with each other.

First off, launch the Android Studio IDE (Integrated development Environment), and create a new project in the same manner as mentioned in our previous tutorials.

Then, select a suitable name for your project. For example, we have selected “Implicit Intent” as our project name. After doing this, set an appropriate package name for your project. For example, we have selected “com.androidaura.intent” as our package name.

In the next step, open the MainActivity.java class file of your project in order to add the following code in it:

Basically, this code develops a template for our whole project. Without this template, no further activity can be performed on our project. For this reason, it is highly important to have this code for our project. By using this code, we have set the basic characteristics of our project, such as giving our project a button by using the Button widget and a space to add and remove text by using the EditText widget.

Apart from adding features in our product, this code also defines what those features would do when they are used. For instance, the code establishes that clicking on the button would initiate the process of delivering the request for finding an app that can be used to open a webpage. In addition to that, we have used ACTION_VIEW as our action on Intent with the webpage URL as the URI (data on which action is performed).

After adding the code in your java class file, you have to open the layout file, i.e. activity_main.xml, in order to add the following code in it:

If you observe the abovementioned code, we have used this file to develop our UI (User Interface) elements. For example, we have used this code to establish that our button would have the text “Submit” written on it. Other than that, we have also set the default state of our text field to have “Enter URL” in order to hint the user what kind of data the project will accept.

Output

After adding the abovementioned codes in both files of your project, your project will be ready to be launched and show you what your codes have achieved. After launching your project, your output should look something like this:

Output

Implicit Intent


Implicit Intent

Not every application can perform every operation. Therefore, we hope that you have understood the significance of having such a useful tool in our Android applications through this tutorial. And with this, we have reached the end of our tutorial. We hope you can now implement Implicit Intent in your application properly.