Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android studio calling button

I am trying to make an on click button which makes phone calls when pressed. Here is my code for java:

public void CampusSafClick(View view){
    Intent callIntent =new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:6038994210"));
    startActivity(callIntent);
}

I understand how to make onclick buttons, so that is not the issue.

I have this code in the manifest:

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

I keep getting the error Unfortunately your app has stop working.

like image 906
Drew Bennett Avatar asked Sep 18 '15 14:09

Drew Bennett


4 Answers

Here is the working code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialContactPhone("123123123");
        }
    });
}

private void dialContactPhone(final String phoneNumber) {
    startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phoneNumber, null)));
}
like image 102
Nikola Milutinovic Avatar answered Nov 18 '22 14:11

Nikola Milutinovic


You need Action_Dial,

use below code it will open Dealer with number specified.

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0123456789"));
startActivity(intent);

The tel: prefix is required, otherwhise the following exception will be thrown: java.lang.IllegalStateException: Could not execute method of the activity.

Action_Dial doesn't require any permission.

If you want to initiate the call directly without user's interaction, you can use action Intent.ACTION_CALL. In this case, you must add the following permission in your AndroidManifest.xml:

<uses-permission android:name="android.permission.CALL_PHONE" />
like image 37
Tabish khan Avatar answered Nov 18 '22 16:11

Tabish khan


I need to enter code above the application list on the manifest:

<uses-permission android:name="android.permission.CALL_PHONE"/>
like image 3
Drew Bennett Avatar answered Nov 18 '22 15:11

Drew Bennett


You can use the following code

intent =new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:+251999999999"));
            startActivity(intent);

and include in manifest file

<uses-permission android:name="android.permission.CALL_PHONE"/>
like image 2
Abdulhakim Zeinu Avatar answered Nov 18 '22 15:11

Abdulhakim Zeinu