Sunday, November 04, 2012

How to make a phone call in Android

In this post, I am going to discuss about on how to make a phone call in android. Just three line statements is enough to make a call in android application.

Just use the below code in button click event to make a phone call.

 Intent callIntent = new Intent(Intent.ACTION_CALL);  
 callIntent.setData(Uri.parse("tel:+919999999999"));  
 startActivity(callIntent);  


And also, you have to set permission in manifest file. Use the below code for phone call permission.

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



Here is the complete code for making a phone call.

Layout File:

 <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" >  
   <Button  
     android:id="@+id/btnPhoneCall"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:layout_alignParentLeft="true"  
     android:layout_centerVertical="true"  
     android:text="Call +919999999999" />  
 </RelativeLayout>  



Activity Class:

 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 PhoneCallActivity extends Activity {  
      Button btnPhoneCall;  
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.phonecall);  
     btnPhoneCall = (Button) findViewById(R.id.btnPhoneCall);  
     btnPhoneCall.setOnClickListener(new OnClickListener() {  
             public void onClick(View v) {  
                  Intent callIntent = new Intent(Intent.ACTION_CALL);  
                  callIntent.setData(Uri.parse("tel:+919999999999"));  
                  startActivity(callIntent);  
              }  
             }  
        );  
   }  
   @Override  
   public boolean onCreateOptionsMenu(Menu menu) {  
     getMenuInflater().inflate(R.menu.phonecall, menu);  
     return true;  
   }  
 }  


Android Manifest File:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.ayyanar.helloworld"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="15" />  
     <uses-permission android:name="android.permission.CALL_PHONE" />  
   <application  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name=".PhoneCallActivity"  
       android:label="@string/title_activity_phone_call" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  



Screenshots:

 

No comments: