-3

I'm building an app that lets you do various tasks and for this particular task. I want the user to be able to call someone. I've taken a look at many questions but all of them tell you to use intents and direct you to the phone app or instead they give you an answer where the users clicks a button and it calls the number assigned in the code.

I want the user to be able to type in a number in and EditText field and be able to hit the call button to call someone, how can I do something similar to this?

I'm not sure if this website is good for stuff like this as answers are hard to come by so I will keep looking around for answers, hopefully someone can answer my q and help me out a bit.

EDIT -

Super disappointed with all the notices and the downvote, really sucks however I appreciate the answers provided. But none helped lol.

So here is how I managed to do what I had asked. I create a onClick event and defined the two main objects with the objects and IDS above it (The EditText and the Button). In the onClick method I got the code to check and see if anything has been entered in the textfield (EditText), it detects with the following code -

(!TextUtils.isEmpty(txtPhone)) {
                    Uri uri = Uri.parse("tel:" + txtPhone); 

If numbers have been typed in, it will run this intent and pull the string from the EditText object.

            Intent intent = new Intent(Intent.ACTION_CALL, uri);
            startActivity(intent);

if nothing is in field it will show a toast. Check below for my method. Hopefully someone in my position will benefit from this and wont have to get downvoted for no particular reason.

// Call button
ImageButton call = (ImageButton) findViewById(R.id.call_button);
final EditText txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
call.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        String txtPhone = txtPhoneNo.getText().toString();
        if (!TextUtils.isEmpty(txtPhone)) {
            Uri uri = Uri.parse("tel:" + txtPhone);
            Intent intent = new Intent(Intent.ACTION_CALL, uri);
            startActivity(intent);
        } else {
            Toast.makeText(getApplicationContext(),
                    "Please enter recipients number", Toast.LENGTH_LONG)
                    .show();
        }
    }
});

Looks like I will be using YouTube for stuff like this now, StackOverFlow is supposed to be about helping new users. Not downvoting their questions and repeatedly labelling their posts as duplicates and already answered.

Pavillion
  • 87
  • 1
  • 9
  • There is no way other than intent for calling. – Hulk Mar 28 '15 at 07:27
  • 1
    You have to use Intent.ACTION_CALL (with number in edittext) as in other answers. That activity only is allowed to make calls with system previlages. – MohK Mar 28 '15 at 07:28
  • @DerGolem Intents isn't what is needed here. – Pavillion Mar 28 '15 at 09:41
  • ... really? If so, please suggest an alternate way. I'm very curious. – Phantômaxx Mar 28 '15 at 10:55
  • @DerGolem None of the answers provided here really helped. Got a notice saying it has been answered which it hasn't. And got -2 on my question It really disappoints me. I know it sucks so thats why I decided to man up and actually do it myself rather than asking again here. I made a field where you can type out the number and get contact suggestions, from there you can hit a button and it will call the number typed in the field automatically. Check my post for the code i created. – Pavillion Mar 28 '15 at 11:10
  • Yes. And... surprise! You are using an `Intent`!! as opposed to: `@DerGolem Intents isn't what is needed here.` Now, seriously: Are you on drugs? – Phantômaxx Mar 28 '15 at 11:17
  • @DerGolem I get what you mean, what I meant was "Direct intents' I dont want my app to directly use an intent to run the phone app. That isn't what is needed. But Thanks anyways. And yes I am on medications, it's for my breathing and lung issue. – Pavillion Mar 28 '15 at 11:21

2 Answers2

1

You should check this : how to make phone call using intent in android? and the accepted answer.

I don't know whether you have searched or not, I found this solution as first link in my search and its pretty much what you want to do (i.e. Call Directly from your app).

Community
  • 1
  • 1
Ankit Bansal
  • 1,801
  • 17
  • 34
1

Try this

handle this on your call button click

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+ editText.getText.toString()));
startActivity(callIntent);

add <uses-permission android:name="android.permission.CALL_PHONE" /> in Manifest

Sony
  • 7,136
  • 5
  • 45
  • 68