Android Implicit Intent

By Arvind Rai, June 24, 2023
Android Implicit Intent can invoke other application in the device. We can open a URL in a browser or can make a call and many other tasks can be performed. Intent has different action that is used with implicit intent. The actions are like Intent.ACTION_VIEW, Intent.ACTION_DIAL, Intent.ACTION_CALL etc. In our demo, we are creating an application which will open a URL in browser and will call a given number. Find the detail description of the demo.

Open URL in Browser using Implicit Intent

Implicit Intent can perform different actions to invoke other existing applications in device. Using implicit intent we can open a URL in a browser. The Intent.ACTION_VIEW action will be used to perform the task. Create implicit intent object passing action and URL and start activity.
Intent implicit = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.concretepage.com"));
startActivity(implicit);
 

Make Phone Call using Implicit Intent

Using implicit intent, we can make call for the given number. To make a phone call there are different actions that can be used.
Intent.ACTION_VIEW : Displays the number to the user. It will display the name from contact number if exits.
Intent.ACTION_DIAL: Displays the number to dial. User explicitly dials the number. It will also display the name from contact number if exits.
Intent.ACTION_CALL: Perform a call for the given number.
We need to add prefix tel: to parse the number.
Intent implicit = new Intent(Intent.ACTION_CALL, Uri.parse("tel:9999887766"));
startActivity(implicit);
 

XML Files

Find the main layout that will create the UI. We are creating two input field and two button.
layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/website"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:ems="5">
    </EditText>
    <Button
        android:id="@+id/runWebsite"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:text="Run WebSite on Browser" />
    <EditText
        android:id="@+id/phNumber"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:ems="5" />
    <Button
        android:id="@+id/dialPhNumber"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:text="Dial Phone Number" />
</LinearLayout>  
App name and other values can be defined in strings.xml. The key defined here can directly be used in layout XML to support i18.
values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">CPApp</string>
   <string name="hello">Hello World!</string>
</resources> 
Now find the android manifest that will configure the activity class and permission to set up the phone call.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:targetApi="31">
        <activity
            android:name="com.cp.android.ImplicitIntentActivity"
            android:label="@string/app_name"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 

Activity Class with Implicit Intent

Find the activity class being used in our example. Here we are performing two tasks. One is opening a URL in browser and second is making a phone call.
ImplicitIntentActivity.java
package com.cp.android;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ImplicitIntentActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button runWebsite = (Button) findViewById(R.id.runWebsite);
		runWebsite.setOnClickListener(new View.OnClickListener() {
	         public void onClick(View view) {
	     		EditText website = (EditText) findViewById(R.id.website);
	     		String strURL = website.getText().toString();
	     		if (strURL.indexOf("http://www") < 0) {
	     			strURL = "http://www." + strURL;
	     		}
	            Intent implicit = new Intent(Intent.ACTION_VIEW, 
	            Uri.parse(strURL));
	            startActivity(implicit);
	         }
	    });
       Button dialPhNumber = (Button) findViewById(R.id.dialPhNumber);
       dialPhNumber.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
	     	EditText phNumber = (EditText) findViewById(R.id.phNumber); 
            Intent implicit = new Intent(Intent.ACTION_CALL, 
            Uri.parse("tel:"+phNumber.getText().toString()));
            startActivity(implicit);
         }
       });
	}
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      return true;
   }
} 

Output

First input box is to enter URL. When click on button, a browser will open with given URL. Second input box is to enter phone number. Click to dial phone number. Application will perform one task at one time.
Android Implicit Intent Example

Reference

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE







©2024 concretepage.com | Privacy Policy | Contact Us