Click here to Skip to main content
Click here to Skip to main content

Android Tutorial - How To Make A Simple Phone Call Application

, 21 Sep 2012
Rate this:
Please Sign up or sign in to vote.
How to make a simple phone call application.

Welcome to my Android tutorial. This time, I'd like to show you how to make a simple phone call application. Is that important? Yes, It is. Imagine, you're in danger, and you don't know what to do. So, what should you do? Call emergency number? Do you remember that number? Or, if you get accident, can you type the number correctly? It's a simple task. But it'll be easier and simpler if you can call a number with one touch. Let's see how to do that.

Create a new android project. First thing we should do is to add user permission in AndroidManifest.xml file so we can make a phone call using our app. See the highlighted code below:

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

 <application android:icon="@drawable/icon" android:label="@string/app_name"
  android:debuggable="true">
  <activity android:name=".PhoneCallSampleAct" 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>
 <uses-permission android:name="android.permission.CALL_PHONE" />
</manifest>

Next, open the main.xml file and we'll change the user interface. We'll add a button to make a phone call.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <Button android:layout_height="wrap_content" android:text="@string/call"
  android:layout_width="wrap_content" android:id="@+id/btnCall"></Button>
</LinearLayout>

Open your activity java file (in my project, it's PhoneCallSampleAct.java) and add this code:

private void phoneCall()
{
   String phoneCallUri = "tel:911";
   Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
   phoneCallIntent.setData(Uri.parse(phoneCallUri));
   startActivity(phoneCallIntent);
}

Finally, let's add the last touch so the app can running. Our scenario is simple. We just need press the button and it'll call 911. Here is the complete code:

package com.blogspot.juniantr.phonecallsample;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class PhoneCallSampleAct extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        View callButton = findViewById(R.id.btnCall);
        callButton.setOnClickListener(this);
    }
    
    private void phoneCall()
    {
     String phoneCallUri = "tel:911";
     Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
     phoneCallIntent.setData(Uri.parse(phoneCallUri));
     startActivity(phoneCallIntent);
    }

 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  switch(v.getId())
  {
   case R.id.btnCall:
    phoneCall();
    break;
   default:
    break;
  }
 }
}

Here is a running sample:

Android Phone Call App Android Phone Call App

Okay, I think that's all I got today. See you in my next post Big Grin | <img src= " src="http://www.codeproject.com/script/Forums/Images/smiley_biggrin.gif" />

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Junian Triajianto
Software Developer
Indonesia Indonesia
Just an ordinary geek.
 
For more information visit my blogs:
Junian Legacy
Kodefun, learning by coding
Follow on   Twitter   Google+

Comments and Discussions

 
QuestionEdit number in application PinmemberMember 1033380113-Oct-13 11:45 
GeneralGood code Pinmemberamon6526-Sep-12 19:43 
SuggestionImage link issue... PinmemberSandip.Nascar20-Sep-12 4:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 21 Sep 2012
Article Copyright 2012 by Junian Triajianto
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid