Call intent in Android
Asked Answered
M

7

9

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

Milksop answered 9/5, 2012 at 5:37 Comment(3)
what is your specific problem?Halfhardy
my app crushes by null pointer exception. can you provide me sample source code to make phone calls??Milksop
Your code is fine, the null pointer is coming from what line? the "String phone" part?Halfhardy
S
27

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.

Sturrock answered 9/5, 2012 at 6:7 Comment(0)
E
11

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);
Eyetooth answered 9/5, 2012 at 5:54 Comment(1)
but also, i have another question :)how i finish the call??it doesn't finish automatically...Milksop
A
5

see below code it may help you.

for call

EditText num = (EditText)findViewById(R.id.number_edit);
String uri = "tel:" + num.trim();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);

for open dialer

Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
dial.setData(Uri.parse("tel:"));
startActivity(dial);
Asti answered 9/5, 2012 at 6:11 Comment(1)
call end worked in my app. see if i use above code then phone dialer open and there is red button for end call. check it again.Asti
C
3
String PhoneNo="+923341234567"
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + PhoneNo));
startActivity(intent);

and add a permission in manifest

<uses-permission android:name="android.permission.CALL_PHONE" />
Corcyra answered 20/1, 2015 at 12:11 Comment(0)
D
2
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(phone));
startActivity(callIntent);

above is the only method you use for calling a phone number in android, when you click on the call button a 'dilerpad' activty is started with perpopulated phone number, and the call will go if you press the call button on the dialer pad.

Dilley answered 9/5, 2012 at 5:45 Comment(5)
you cant make call by directly clicking on the call button, in turn it should go to the dialer pad activity.Dilley
Actually you can, with the permission to call. There are 2 Actions, ACTION_DIAL & ACTION_CALL : Where action dial takes you to the dialer with the number and call sends the call out.Halfhardy
developer.android.com/reference/android/content/… Read that.Halfhardy
@JoxTraex wait i wll check it outDilley
in what place i must insert this intent?Milksop
B
0
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" +"93");
intent.setData(Uri.parse(uri));
startActivity(intent);
Bursitis answered 22/7, 2017 at 9:42 Comment(0)
C
0

Try this

EditText num = (EditText)findViewById(R.id.phone_number);
String uri = "tel:" + num.trim();
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(intent);
Chanel answered 22/7, 2017 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.