Sunday 20 October 2013

Telephony API in Android

How to make phone call from my android application?

To make calling application is very easy. you just required to call an implicit Intent named Intent.ACTION_CALL.
Intent.ACTION_CALL is responsible to call some specific no.

Example:

   Create an Android Application project named PhoneCall.



step1: Code for layout xml file: 


    android:id="@+id/LinearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter your no." />

   
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Call" />


step 2 : code for Activity source java file.


package in.androidshivendra.phonecall

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;
//import android.widget.EditText;


public class PhoneCallActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    EditText edtPhone ;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button callButton =(Button) findViewById(R.id.button1);
     callButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub

phonecall();
}
})
        
    }
    
public void phoneCall(){
   
    try{
   
   
   
    edtPhone = (EditText) findViewById(R.id.edtPhone);
    String phonecall = "tel:+"+edtPhone.getText().toString();
   
//     String phonecall = "tel:+5558"; fixed no by this way if you not want to take no from editbox.
   

  Intent phonecallintent = new Intent(Intent.ACTION_CALL);
  
  phonecallintent.setData(Uri.parse(phonecall));

    startActivity(phonecallintent);
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    

}
  

Step 3: you have to add android.permission.CALL_PHONE permission in manifest file by this way.

uses-permission android:name="android.permission.CALL_PHONE"

step4: Now your application is ready for run. Just Test it.

For testing purpose you just start two emulator that port no is may be 5554 and 5556. you run this application in first emulator and in edit box put the port no of second emulator then first emulator generate call for second emulator.





No comments:

Today's Pageviews