Permissions to make calls compatible with Android 6.0 and earlier versions

9

In my app, I am creating a Activity with several TextView that collects the information from a database in SQLite integrated in my app, and one of the data is the phone.

My intention with this data, was to be able to create a button in the same Activity , that when pressed, it will make the call to the phone of the BD record that is shown on the screen, but I have found that for each Android version is done in one way, for example:

For Android 5.0:

Intent intent = new Intent(Intent.ACTION_CALL);         
intent.setPackage("com.android.phone");
intent.setData(Uri.parse("tel:" + number));
context.startActivity(intent);

But for Android 6.0 I have no idea how to do it and of course, not only has to be for the latest version, but it has to be compatible with the others.

My question is:

How do I create a permission to make calls compatible with Android 6.0 and earlier?

I hope you can give me a hand and an example. :)

Thanks to everyone in advance.

    
asked by Vicky Vicent 09.06.2016 в 21:45
source

2 answers

4

I'll give you the example of the link developer.android.com/training/ permissions / requesting.htm I just did it, taking advantage of the fact that I will also have to implement it.

Request permissions only from 6.0:

final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.CALL_PHONE)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CALL_PHONE)) {

        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    MY_PERMISSIONS_REQUEST_CALL_PHONE);
        }
    }
} 

Receive the user's response:

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                callPhone();
            } else {
                System.out.println("El usuario ha rechazado el permiso");
            }
            return;
        }
    }
}

public void callPhone() {
    Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setPackage("com.android.phone");
    intent.setData(Uri.parse("tel:" + number));
    context.startActivity(intent);
}

I tried it and it goes well anyway now I will also try the example of @Elenasys. Greetings.

    
answered by 09.06.2016 / 22:18
source
2

For applications with OS Android prior to 6.0 it is sufficient to define the permission within your file AndroidManifest.xml :

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

Require permissions on Android 6.0 or later.

This would be the way to require permissions to be able to make phone calls in Android 6.0 :

 int permissionCheck = ContextCompat.checkSelfPermission(
            this, Manifest.permission.CALL_PHONE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        Log.i("Mensaje", "No se tiene permiso para realizar llamadas telefónicas.");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 225);
    } else {
        Log.i("Mensaje", "Se tiene permiso!");
    }
    
answered by 09.06.2016 в 22:12