Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call intent in Android

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

like image 882
user1383729 Avatar asked May 09 '12 05:05

user1383729


People also ask

How do you call an Intent?

Intent Object - Action to make Phone CallIntent phoneIntent = new Intent(Intent. ACTION_CALL); You can use ACTION_DIAL action instead of ACTION_CALL, in that case you will have option to modify hardcoded phone number before making a call instead of making a direct call.

What does an Intent do in Android?

An Intent object carries information that the Android system uses to determine which component to start (such as the exact component name or component category that should receive the intent), plus information that the recipient component uses in order to properly perform the action (such as the action to take and the ...

What is Intent in android with example?

The intent is a messaging object which passes between components like services, content providers, activities, etc. Normally startActivity() method is used for invoking any activity. Some of the general functions of intent are: Start service.


1 Answers

I think you missed the "tel:" part in the URI.

Replace the following..

Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse(phone));
        startActivity(callIntent);

with

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

or

Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:"+phone));
            startActivity(callIntent);
like image 185
Shashank Kadne Avatar answered Sep 28 '22 10:09

Shashank Kadne