Home  >  Android

Android Implicit Intent Example | Open URL in Browser | Make Phone Call

By Arvind Rai, October 11, 2014
Implicit Intent in Android can invoke other application in the device. We can open a URL in a browser or can make a call. 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. 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 for Layout, Values and Android Manifest

Find the main layout that will create the UI. We are creating to 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"
    package="com.cp.android"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.cp.android.ImplicitIntentActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 

Activity Class for Implicit Intent

Find the activity class being used in 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

To check the output, install APK file. We will see below UI. First input box is to enter URL and then 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 | Open URL in Browser | Make Phone Call
In this way, we are done. You can download source file and APK file directly from the given link.

Download Source Code

Download APK file

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us