Android Explicit Implicit Intent Example

Intent is an important way to interact between components in an Android application. It can not only specify the actions that the current component wants to perform, but also transfer data between different components. An Intent can generally be used to launch activity, service and send broadcast.

1. Intent Classification.

  1. Explicit intent : This kind of intent will define the target component (Activity, Service etc) directly. So only the specified target component will be invoked.
  2. Implicit Intent : This kind of Intent will not specify the component ( Activity, Service etc ) directly. It only specify the action and category information. Then android OS will filter out and start the component which can response to the action and category.

2. Explicit Intent Example.

There are two Activity in below example. When you click the button in the first activity, it will use an explicit Intent to tell android OS to start the second Activity immediately. And transfer some string data to the second activity also.

When you click the bottom back menu while the second activity is active, the second activity will be closed and the first activity will be shown again.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/4TWiFNwf4Bk

2.1 AndroidManifest.xml

<!-- This is the main Activity. -->
<activity android:name=".IntentExampleActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<!-- This Activity will be invoked use explicit Intent. -->
<activity android:name=".IntentExampleExplicitActivity" />

2.2 Main Activity Java File.

IntentExampleActivity.java

package com.dev2qa.example;

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

public class IntentExampleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_example);

        // Set application logo icon and title.
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setIcon(R.drawable.icon_movie_32);
        actionBar.setTitle("dev2qa.com - Explicit And Implicit Intent Example");

        this.demoExplicitIntent();
    }

    private void demoExplicitIntent()
    {
        // Click this button will send an Intent to system to start IntentExampleExplicitActivity.
        Button sendExplicitIntentButton = (Button)findViewById(R.id.sendExplicitIntentButton);
        sendExplicitIntentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Create a explicit Intent.
                Intent explicitIntent = new Intent(IntentExampleActivity.this, IntentExampleExplicitActivity.class);
                // Transfer data to target Activity.
                explicitIntent.putExtra("From", "This Intent come from IntentExampleActivity.");
                explicitIntent.putExtra("Purpose", "Demo Explicit Intent.");
                // Start the activity use above Intent.
                startActivity(explicitIntent);
            }
        });
    }
}

2.3 Main Layout Xml File.

activity_intent_example.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/sendExplicitIntentButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send Explicit Intent" />

</LinearLayout>

2.4 Target Activity Java File.

IntentExampleExplicitActivity.java

package com.dev2qa.example;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class IntentExampleExplicitActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_example_explicit);

        setTitle("dev2qa.com - Explicit Intent Started Activity");

        // Retrieve transferred data from Intent.
        String from = getIntent().getStringExtra("From");
        String purpose = getIntent().getStringExtra("Purpose");

        // Show Intent transfer data in TextView.
        TextView intentExtraFromTextView = (TextView)findViewById(R.id.intentExtraFromTextView);
        intentExtraFromTextView.setTextSize(20);
        intentExtraFromTextView.setText("From : " + from);

        TextView intentExtraPurposeTextView = (TextView)findViewById(R.id.intentExtraPurposeTextView);
        intentExtraPurposeTextView.setTextSize(20);
        intentExtraPurposeTextView.setText("Purpose : " + purpose);
    }
}

2.5 Target Activity Layout Xml File.

activity_intent_example_explicit.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/intentExtraFromTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/intentExtraPurposeTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

3. Implicit Intent Example.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/zK77O-jzxZ4

3.1 AndroidManifest.xml

To make an Activity response implicit Intent, we need add <action> and <category> sub xml element under <intent-filter> as below.

In <action> tag, you need to specify which action this activity can response. You can add multiple <action> for one Activity.

The <category> tag include more detail information that this Activity can response.

Only the intent specified action and category value both match <action> and <category> data settings here, then the Activity will be started.

The action and category name value can be android os built-in value, or you can use your customized value also.

<!-- This is the main Activity. -->
<activity android:name=".IntentExampleActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<!-- This Activity will be invoked use implicit Intent. -->
<activity android:name=".IntentExampleImplicitActivity">
    <intent-filter>
        <!-- This activity can response below action and category values Intent. -->
        <action android:name="android.intent.action.DIAL" />
        <action android:name="com.dev2qa.example.IntentExampleActivity.CUSTOM_ACTION" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="com.dev2qa.example.IntentExampleActivity.CUSTOM_CATEGORY" />
    </intent-filter>
</activity>

3.2 Main Activity Java File.

IntentExampleActivity.java

package com.dev2qa.example;

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

public class IntentExampleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_example);

        // Set application logo icon and title.
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setIcon(R.drawable.icon_movie_32);
        actionBar.setTitle("dev2qa.com - Explicit And Implicit Intent Example");

        this.demoBuiltInImplicitIntent();

        this.demoCustomImplicitIntent();

        this.demoOpenBrowserImplicitIntent();

        this.demoPhoneCallImplicitIntent();
    }

    private void demoBuiltInImplicitIntent()
    {
        // Click this button will send an android os built-in action implicit Intent,
        // android os will start the Activity which can response this Intent.
        Button sendBuiltInImplicitIntentButton = (Button)findViewById(R.id.sendBuiltInImplicitIntentButton);
        sendBuiltInImplicitIntentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Intent.ACTION_DIAL is the os buitl-in action.
                Intent implicitIntent = new Intent(Intent.ACTION_DIAL);
                implicitIntent.putExtra("Message", "This is a implicit Intent.Action is Intent.ACTION_DIAL");
                // Because our IntentExampleImplicitActivity activity also register to this action, 
                // so it will be listed in the popup app list, user can select it to response this action.
                startActivity(implicitIntent);
            }
        });
    }

    private void demoCustomImplicitIntent()
    {
        // Click this button will send an custom action implicit Intent,
        // android os will start the Activity which can response this Intent.
        Button sendCustomImplicitIntentButton = (Button)findViewById(R.id.sendCustomImplicitIntentButton);
        sendCustomImplicitIntentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Create Intent with custom action and category.
                Intent implicitIntent = new Intent("com.dev2qa.example.IntentExampleActivity.CUSTOM_ACTION");
                implicitIntent.addCategory("com.dev2qa.example.IntentExampleActivity.CUSTOM_CATEGORY");
                implicitIntent.putExtra("Message", "This is a implicit Intent.Action is com.dev2qa.example.IntentExampleActivity.CUSTOM_ACTION");
                startActivity(implicitIntent);
            }
        });
    }

    private void demoOpenBrowserImplicitIntent()
    {
        // Click this button will open a browser use implicit Intent,
        Button openBrowserImplicitIntentButton = (Button)findViewById(R.id.openBrowserImplicitIntentButton);
        openBrowserImplicitIntentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Create Intent with Intent.ACTION_VIEW.
                Intent implicitIntent = new Intent(Intent.ACTION_VIEW);
                implicitIntent.setData(Uri.parse("http://www.bing.com"));
                startActivity(implicitIntent);
            }
        });
    }

    private void demoPhoneCallImplicitIntent()
    {
        // Click this button will open phone call dial screen use implicit Intent,
        Button openPhoneCallImplicitIntentButton = (Button)findViewById(R.id.openPhoneCallImplicitIntentButton);
        openPhoneCallImplicitIntentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Create Intent with Intent.ACTION_VIEW.
                Intent implicitIntent = new Intent(Intent.ACTION_DIAL);
                implicitIntent.setData(Uri.parse("tel:1100011"));
                startActivity(implicitIntent);
            }
        });
    }
}

3.3 Main Layout Xml File.

activity_intent_example.xml

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/sendBuiltInImplicitIntentButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Send Build-In Implicit Intent" />

        <Button
            android:id="@+id/sendCustomImplicitIntentButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Send Custom Implicit Intent" />

        <Button
            android:id="@+id/openBrowserImplicitIntentButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Open Browser Use Implicit Intent" />

        <Button
            android:id="@+id/openPhoneCallImplicitIntentButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Open Phone Call Use Implicit Intent" />

    </LinearLayout>

3.4 Target Activity Java File.

IntentExampleImplicitActivity.java

package com.dev2qa.example;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class IntentExampleImplicitActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_example_implicit);

        setTitle("dev2qa.com - Implicit Intent Started Activity");

        // Get Intent transfer data.
        String message = getIntent().getStringExtra("Message");

        // Show Intent transfer data in TextView.
        TextView implicitIntentExtraTextView = (TextView)findViewById(R.id.implicitIntentExtraTextView);
        implicitIntentExtraTextView.setTextSize(20);
        implicitIntentExtraTextView.setText(message);
    }
}

3.5 Target Activity Layout Xml File.

activity_intent_example_implicit.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/implicitIntentExtraTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.