Android Implicit Intents with Examples

In android, Implicit Intents won’t specify any name of the component to start instead, it declare an action to perform and it allows a component from other apps to handle it. For example, by using implicit intents we can request another app to show the location details of the user or etc.

 

Following is the pictorial representation of how Implicit intents send a request to the android system to start another activity.

 

Android Implicit Intents Process Flow

 

If you observe the above image Activity A creates an intent with the required action and sends it to an android system using the startActivity() method. The android system will search for an intent filter that matches the intent in all apps. Whenever the match found the system starts matching activity (Activity B) by invoking the onCreate() method.

 

In android when we create implicit intents, the android system will search for matching components by comparing the contents of intent with intent filters which defined in the manifest file of other apps on the device. If the matching component found, the system starts that component and sends it to the Intent object. In case, if multiple intent filters are matched then the system displays a dialog so that the user can pick which app to use.

 

In android, an Intent Filter is an expression in the app’s manifest file and it is used to specify the type of intents that the component would like to receive. In case if we create an Intent Filter for activity, there is a possibility for other apps to start our activity by sending a certain type of intent otherwise the activity can be started only by an explicit intent.

 

Following is the simple code snippet of implicit intent in the android application.

 

Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(
"http://www.tutlane.com"));
startActivity(intent);

If you observe above implicit intent we didn’t defined any specific name of component to start, instead we defined an action (ACTION_VIEW) to open the defined URL (http://www.tutlane.com) in browser within the device.

Android Implicit Intent Example

Following is the complete example of implementing an implicit intent in the android application.

 

Create a new android application using android studio and open an activity_main.xml file from \src\main\res\layout path. In case if you are not aware of creating an app in android studio check this article Android Hello World App.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context="com.tutlane.intents.MainActivity">
    <
EditText
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:id="@+id/urlText"
       
android:layout_alignParentTop="true"
       
android:layout_centerHorizontal="true"
       
android:layout_marginTop="100dp"
       
android:ems="10" />
    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:id="@+id/btnNavigate"
       
android:layout_below="@+id/urlText"
       
android:text="Navigate"
       
android:layout_centerHorizontal="true" />
</
RelativeLayout>

Now open the main activity file MainActivity.java from \src\main\java\com\tutlane\com.tutlane.helloworld path and write the following code to open a new browser window on button click to load given url.

MainActivity.java

package com.tutlane.intents;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
final EditText editText = (EditText)findViewById(R.id.urlText);
        Button btn = (Button) findViewById(R.id.
btnNavigate);
        btn.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                String url =
editText.getText().toString();
                Intent intent =
new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
            }
        });
    }
}

Output of Android Implicit Intent Example

When we run above example using android virtual device (AVD) we will get a result like as shown below

 

Android Implicit Intents Example Result or Output

 

When we enter the website URL and click on the button it will open a website in a new browser window in the same application.

 

This is how we can use Android Implicit Intents in applications based on our requirements.