Call intent in Android

user1383729 picture user1383729 · May 9, 2012 · Viewed 50.6k times · Source

How can I make call by pressing button? I get my number as a string from EditText. Here is my sample code:

String phone = editPhone.getText().toString();
btnPhone.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                call();
            }
        });
public void call() {
    try {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse(phone));
        startActivity(callIntent);
    } catch (ActivityNotFoundException activityException) {
         Log.e("myphone dialer", "Call failed", e);
    }
}

I added all permissions to manifest file.

but I am getting NullPointerexception

Answer

MKJParekh picture MKJParekh · May 9, 2012

This simple approach should work for you.

Ex.

public class CallActivity extends Activity{
   String phone = "";

   onCreate()
   {
        btnPhone.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View arg0) { 
                phone = editPhone.getText().toString(); 
                call(); 
            } 
        });    
   }

   public void call() {   
            Intent callIntent = new Intent(Intent.ACTION_CALL);          
            callIntent.setData(Uri.parse("tel:"+phone));          
            startActivity(callIntent);  
   }
}

You might be using String variable phone out of scope.