How to grant permits in a fragment?

1

I am working on an app that needs to make calls, my problem is that I do not make the calls because I do not have the permission, how do I grant the permissions within a fragment?

    
asked by Nelson Emmanuel 23.03.2017 в 23:22
source

2 answers

1

The permission to make calls on devices with OS 6.0+ must be done manually, it is not enough to add it in the AndroidManifest.xml:

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

Granting permission is not only for a Fragment or a Activity , it is actually granted for the entire application.

This would be the way to do it:

   //Defines una variable para el request code:
   private static final int REQUEST_CODE_ASK_PERMISSIONS = 507;

This snippet can be defined at the start of your application, for example within onCreate() of your Activity , or Activity that contains or performs the transaction of Fragmento :

     //Se realiza la petición de permisos para dispositivos con OS >= 6.0
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            // Se tiene permiso
            Intent calIntent = new Intent(Intent.ACTION_CALL);
            calIntent.setData(Uri.parse("tel:5553061234"));
            startActivity(calIntent);
        }else{
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE_ASK_PERMISSIONS);
            return;
        }
    }else{
        // No se necesita requerir permiso, OS menor a 6.0.
    }

in the same Activity you implement the method:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case REQUEST_CODE_ASK_PERMISSIONS:
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                // El usuario acepto los permisos.
                Toast.makeText(this, "Gracias, aceptaste los permisos requeridos para el correcto funcionamiento de esta aplicación.", Toast.LENGTH_SHORT).show();
            }else{
                // Permiso denegado.
                Toast.makeText(this, "No se aceptó permisos", Toast.LENGTH_SHORT).show();
            }
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

This will be enough to subsequently carry out the telephone call without problems:

        Intent calIntent = new Intent(Intent.ACTION_CALL);
        calIntent.setData(Uri.parse("tel:5553061234"));
        startActivity(calIntent);
    
answered by 24.03.2017 в 17:07
0

I do not have much idea of Android, but I was doing several courses. I remember that when you do not have permissions you must request them to the method requestPermissions() to request the permissions that you need. That is, in the try you can put a call to what you need for your app, for example contacts.

Although it is best to request the permissions once the user opens the app, that is, specifying what you need in AndroidManifest.xml , so you should not then specify permissions in a particular fragment.

Something like this:

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

More info: Permissions in Android

    
answered by 23.03.2017 в 23:40