Thursday, July 14, 2011

Android Tutorial - 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 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 the 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 :D

7 comments:

  1. umur brp hari...? bru 149 view ya.. mntap artikelna...

    ReplyDelete
  2. @Perru Merah:
    Thanks :)
    This blog age isn't a week yet, and soon I'll write more high quality articles :)

    ReplyDelete
  3. why the heck would you want to call the emergency services?

    ReplyDelete
  4. app development companiesJuly 20, 2013 at 2:27 PM

    This is really informative and interesting, Its full of information.Thanks for sharing with us...

    http://www.aleedex.org

    ReplyDelete
  5. Thank for u sample .. but I can not use "tel:*124#"
    is it available to use # key and * key

    Thank

    ReplyDelete
  6. Hi Junian, I'm not really a coder, but could you share if this call is made INSIDE the application? I'm looking for a way to make a 911 call while giving the caller the option to check the emergency details on the phone (it's an app for emergency situations). Normally you'll see the android system, thus making it difficulty to read from the app.

    ReplyDelete

Please leave your comment here

Back to top