Android Implicit Intents with Examples

  Total Views : 78
  Comments : 0
  Keywords : Android implicit intents are used to specify an action that can invoke by any app on the device to perform an action, Android implicit intents won’t specify any name of app component

Here we will learn implicit intents in android with examples and how to use android implicit intents in applications to invoke an action and how that action can handled by other apps with examples.

Android Implicit Intents

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

 

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

 

Android Implicit Intents Process Flow

 

If you observe above image Activity A creates an intent with required action and send it to an android system using 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 onCreate() method.

 

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

 

In android, Intent Filter is an expression in 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 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 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 website url and click on button it will open a website in new browser window in same application.

 

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

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User