How to make a call programmatically in android

watch_later Wednesday, July 27, 2016
Hey,In this tutorial we can learn about how to make a call programmatically  by using intents concept  in android.It is very easy to implement.

There are two types of intents in android

1) Implicit Intent

2) Explicit Intent

By using Implicit Intent we can make call programmatically

Lets start coding

Create a project:

firstly we can give permission in AndroidManifest.xml

  <uses-permission android:name="android.permission.CALL_PHONE" />


AndroidManifest.xml 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.forblog"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
       <uses-permission android:name="android.permission.CALL_PHONE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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>


MainActivity.java

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

String phonenum="8888888888";  //you can give any phone number
   Intent i= new Intent(Intent.ACTION_CALL);
   i.setData(Uri.parse("tel:" + phonenum));
   startActivity(i);

}
}

Demo:

Finally Run your application

OUTPUT:

after running your application  calling automatically  to specified number in code

Happy in coding..





sentiment_satisfied Emoticon