How to make a phone call from your application


SDK Version: 
M3

To enable your application to initiate a phone call, you must set permissions in the manifest file:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="com.bubudsadasdas"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".phonecalls"
  8.                   android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>
  14.  
  15.     </application>
  16.     <uses-sdk android:minSdkVersion="3" />
  17. <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
  18. </manifest>

Now create a new activity with a call method. It should look like this:

  1. private void call() {
  2.     try {
  3.         Intent callIntent = new Intent(Intent.ACTION_CALL);
  4.         callIntent.setData(Uri.parse("tel:123456789"));
  5.         startActivity(callIntent);
  6.     } catch (ActivityNotFoundException e) {
  7.         Log.e("helloandroid dialing example", "Call failed", e);
  8.     }
  9. }

What happens when you start a phone call depends, in part, on the telephone network. The number may be incorrect. The network may be busy or otherwise unavailable. The call can be interrupted. Here, however, you see no error-handling logic, except for catching and logging exceptions that can be thrown if Android's system encounters a problem when finding applications that can process Intent objects.

Now call the call :) method in an empty activity:

  1. import android.app.Activity;
  2. import android.content.ActivityNotFoundException;
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7.  
  8. public class phonecalls extends Activity {
  9.     /** Called when the activity is first created. */
  10.     @Override
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.main);
  14.         call();
  15.     }
  16.  
  17. private void call() {
  18.     try {
  19.         Intent callIntent = new Intent(Intent.ACTION_CALL);
  20.         callIntent.setData(Uri.parse("tel:123456789"));
  21.         startActivity(callIntent);
  22.     } catch (ActivityNotFoundException activityException) {
  23.          Log.e("helloandroid dialing example", "Call failed", e);
  24.     }
  25. }
  26.  
  27. }



Feel free to download the complete source code from here.