Wednesday, December 18, 2013

How to make phone call from android program: Android tutorial

Program description:

This is a demo program on how to make a phone call from an android application on button click.

For this we will use ACTION_CALL in the intent, and pass the the Uri which contains phone number to make the call. On clicking the button it will launch the call application.

Note : It requires android permission "android.permission.CALL_PHONE".

First activity


package com.techpalle.b15_call;

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

public class MainActivity extends Activity {
 Button b;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  b = (Button) findViewById(R.id.button1);
  b.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent in = new Intent();
    in.setAction(Intent.ACTION_CALL);
    Uri myUri = Uri.parse("tel:12345");
    in.setData(myUri);
    startActivity(in);
   }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}
xml file for first activity 
File name : activity_main.xml
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   tools:context=".MainActivity" >  
   <Button  
     android:id="@+id/button1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentLeft="true"  
     android:layout_alignParentTop="true"  
     android:layout_marginLeft="60dp"  
     android:text="Call" />  
 </RelativeLayout>  

Download complete code : Click to download

Tags: android, make phone call

No comments:

Post a Comment