6

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.

Drew Bennett
  • 483
  • 3
  • 6
  • 25

6 Answers6

12

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)));
}
Nikola Milutinovic
  • 731
  • 1
  • 8
  • 23
  • 1
    Thanks Nikola for the code. I just needed to put the uses permission above the application list instead of inside of it. – Drew Bennett Sep 18 '15 at 14:53
4

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" />
S.Spieker
  • 7,005
  • 8
  • 44
  • 50
Tabish khan
  • 772
  • 8
  • 11
3

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

<uses-permission android:name="android.permission.CALL_PHONE"/>
Drew Bennett
  • 483
  • 3
  • 6
  • 25
2

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"/>
Abdulhakim Zeinu
  • 3,333
  • 1
  • 30
  • 37
1

For make a call using android, It can be implement using Intents.

public void MakePhoneCall(View view){
    Intent callIntent =new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:9961907453"));
    if (ActivityCompat.checkSelfPermission(MainActivity.this,
           Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
              return;
           }
           startActivity(callIntent);
}

I have this code in the manifest:

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

If you are using SDK version greater than Lolipop then you should include request permission.

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
1
ivCall.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (serviceListDates.get(position).getUser_mobile().length() != 0) {
            final AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
            alertDialog.setTitle("NKA SERVICE");
            alertDialog.setMessage("Do you want to Call ?");
            alertDialog.setIcon(R.drawable.call_icon);
            alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    ((DeliveredServiceOilActivity) mContext).callPhoneNumber
                            (serviceListDates.get(position).getUser_mobile());
                }
            });`enter code here
            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            alertDialog.show();
        } else
            AlertUtils.SHOW_TOAST(mContext, mContext.getString(R.string.please_add_number));
    }
});
Sami Ahmed Siddiqui
  • 2,328
  • 1
  • 16
  • 29