AdSense

AdSense3

Wednesday, 21 October 2015

How to make a phone call in android

We are able to make a phone call in android via intent. You need to write only three lines of code to make a phone call.

  1. Intent callIntent = new Intent(Intent.ACTION_CALL);  
  2. callIntent.setData(Uri.parse("tel:"+8802177690));//change the number  
  3. startActivity(callIntent);  

Example of phone call in android

activity_main.xml

Drag the EditText and Button from the pallete, now the activity_main.xml file will like this:
File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/button1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="118dp"  
  14.         android:text="Call" />  
  15.   
  16.     <EditText  
  17.         android:id="@+id/editText1"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_alignParentTop="true"  
  21.         android:layout_centerHorizontal="true"  
  22.         android:layout_marginTop="25dp"  
  23.         android:ems="10" />  
  24.   
  25. </RelativeLayout>  

Write the permission code in Android-Manifest.xml file

You need to write CALL_PHONE permission as given below:
  1. <uses-permission android:name="android.permission.CALL_PHONE" />  
File: Android-Manifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <manifest xmlns:androclass="http://schemas.android.com/apk/res/android"  
  4.     package="com.example.phonecall"  
  5.     android:versionCode="1"  
  6.     android:versionName="1.0" >  
  7.   
  8.     <uses-sdk  
  9.         android:minSdkVersion="8"  
  10.         android:targetSdkVersion="16" />  
  11.   
  12.     <uses-permission android:name="android.permission.CALL_PHONE" />  
  13.     <application  
  14.         android:allowBackup="true"  
  15.         android:icon="@drawable/ic_launcher"  
  16.         android:label="@string/app_name"  
  17.         android:theme="@style/AppTheme" >  
  18.         <activity  
  19.             android:name="com.example.phonecall.MainActivity"  
  20.             android:label="@string/app_name" >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.   
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.     </application>  
  28.   
  29. </manifest>  

Activity class

Let's write the code to make the phone call via intent.
File: MainActivity.java
  1. package com.example.phonecall;  
  2.   
  3. import android.net.Uri;  
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     EditText edittext1;  
  15.     Button button1;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.   
  21.         //Getting the edittext and button instance  
  22.         edittext1=(EditText)findViewById(R.id.editText1);  
  23.         button1=(Button)findViewById(R.id.button1);  
  24.   
  25.         //Performing action on button click  
  26.         button1.setOnClickListener(new OnClickListener(){  
  27.   
  28.             @Override  
  29.             public void onClick(View arg0) {  
  30.                 String number=edittext1.getText().toString();  
  31.                 Intent callIntent = new Intent(Intent.ACTION_CALL);  
  32.                 callIntent.setData(Uri.parse("tel:"+number));  
  33.                 startActivity(callIntent);  
  34.             }  
  35.               
  36.         });  
  37.     }  
  38.   
  39.     @Override  
  40.     public boolean onCreateOptionsMenu(Menu menu) {  
  41.         // Inflate the menu; this adds items to the action bar if it is present.  
  42.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  43.         return true;  
  44.     }  
  45.       
  46. }  

Install and Run the apk file on the Real Device (e.g. Mobile) to make the phone call.


Output:

android phone call example output 1

android phone call example output 2

No comments:

Post a Comment