Android dial a phone number programmatically

2020-05-29 19:50发布

问题:

How do I dial a number programmatically from the Android application? I don't want to make a call, which I know I can do by making an intent: new Intent(Intent.ACTION_CALL), I just want to take the user to the Android dial prompt, with the number already input by passing it through the intent, so that she has the option to call that number herself.

回答1:

Use the below code:

Uri number = Uri.parse("tel:123456789");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
startActivity(callIntent);


回答2:

Use ACTION_DIAL

Like,

Intent intent = new Intent(Intent.ACTION_DIAL);

Activity Action: Dial a number as specified by the data. This shows a UI with the number being dialed, allowing the user to explicitly initiate the call.



回答3:

Intent intent = new Intent(Intent.ACTION_CALL);

intent.setData(Uri.parse("tel:" + bundle.getString("mobilePhone")));
context.startActivity(intent);

Don't forget to add the relevant permission to your manifest:

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


回答4:

This code will make a call without user interaction and will also pick up the default phone dialler

    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + url));
    intent.setClassName("com.android.phone","com.android.phone.OutgoingCallBroadcaster");
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

IF you wants the user to choose his dialler remove

intent.setClassName("com.android.phone","com.android.phone.OutgoingCallBroadcaster");

If you want just to throw the number to the dialler and the user press the call button use this code (I believe thats what you are asking for )

    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + url));
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

Where the url is the phone number and dont forget to write the permission in the manifest file .

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


回答5:

I had a similar problem which was solved by

        btn.setOnClickListener(new  View.OnClickListener(){
        public void onClick(View v) {
            String q=(t.getText().toString());
            Uri u=Uri.parse("tel:"+q);

            Intent i=new Intent(Intent.ACTION_VIEW,u);
            startActivity(i);
        }
    });


回答6:

This code will dial your number without mobile phones default calling keypad:

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + textView.getText())); 
intent.setClassName("com.android.phone","com.android.phone.OutgoingCallBroadcaster");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);