1

Dans mon AndroidManifest.xml:boîtes de dialogue d'autorisation

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

Mais quand je l'ai installé mon App-APK, je dois mise en place des autorisations dans mon Android OS dans les paramètres manuellement (Paramètres> Applications> myApp> Permissions > Stockage).

Comment puis-je obtenir les autorisations de configuration automatiquement. Ou l'utilisateur de mon application doit le confirmer dans une boîte de dialogue?

+1

Je pense que cela dépend de la targetSdkVersion définie dans votre fichier Gradle. Sur les versions basses (23 <), l'autorisation, une fois dans le manifeste, est automatiquement donnée. Sur les versions supérieures, vous devez demander cette autorisation en cours d'exécution. Lisez à propos de la gestion des autorisations dans les documents android. – JacksOnF1re

+0

Vous voulez dire ça? https://developer.android.com/training/permissions/requesting.html – SilverBlue

+0

oui - (et 12 autres caractères, pour ajouter un commentaire) – JacksOnF1re

Répondre

1

J'utilise le code suivant pour obtenir l'autorisation dans mon application chaîne

<string name="permissions_title">Permissions</string> 
<string name="draw_over_permissions_message">To display Audio Widget app needs the permission to draw over another apps.</string> 
<string name="read_ext_permissions_message">To load list of music app needs access to your media files.</string> 
<string name="btn_continue">Continue</string> 
<string name="btn_cancel">Cancel</string> 
<string name="toast_permissions_not_granted">Permissions not granted.</string> 

java

@TargetApi(Build.VERSION_CODES.M) 
private void checkReadStoragePermission() { 
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)) { 
      DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        if (which == DialogInterface.BUTTON_POSITIVE) { 

         ActivityCompat.requestPermissions(ParentActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, EXT_STORAGE_PERMISSION_REQ_CODE); 


        } else if (which == DialogInterface.BUTTON_NEGATIVE) { 
         onPermissionsNotGranted(); 

        } 
        dialog.dismiss(); 
        finish(); 
        startActivity(getIntent()); 
       } 
      }; 
      new AlertDialog.Builder(this) 
        .setTitle(R.string.permissions_title) 
        .setMessage(R.string.read_ext_permissions_message) 
        .setPositiveButton(R.string.btn_continue, onClickListener) 
        .setNegativeButton(R.string.btn_cancel, onClickListener) 
        .setCancelable(false) 
        .show(); 
      return; 
     } 
     ActivityCompat.requestPermissions(ParentActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.READ_PHONE_STATE}, EXT_STORAGE_PERMISSION_REQ_CODE); 
     return; 
    } 

} 

private void onPermissionsNotGranted() { 
    Toast.makeText(this, R.string.toast_permissions_not_granted, Toast.LENGTH_SHORT).show(); 
    Log.v("tom", "JERRY"); 
} 


@TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
private void checkwriteStoragePermission() { 
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 
      DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        if (which == DialogInterface.BUTTON_POSITIVE) { 

         ActivityCompat.requestPermissions(ParentActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE); 
         Log.v("tom", "TOM"); 
        } else if (which == DialogInterface.BUTTON_NEGATIVE) { 
         onPermissionsNotGranted(); 
         Log.v("tom", "JERRY"); 
        } 
        dialog.dismiss(); 
       } 
      }; 
      new AlertDialog.Builder(this) 
        .setTitle(R.string.permissions_title) 
        .setMessage(R.string.read_ext_permissions_message) 
        .setPositiveButton(R.string.btn_continue, onClickListener) 
        .setNegativeButton(R.string.btn_cancel, onClickListener) 
        .setCancelable(false) 
        .show(); 
      return; 
     } 
     ActivityCompat.requestPermissions(ParentActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE); 
     return; 
    } 

} 

avec

private static final int WRITE_EXTERNAL_STORAGE = 4; 
private static final int READ_PHONE_STATE = 3; 

et vous pouvez appeler la méthode où vous voulez.

+0

Je reçois une erreur: Impossible de résoudre le symbole 'EXT_STORAGE_PERMISSION_REQ_CODE' – SilverBlue

+0

êtes-vous en mesure de résoudre le problème? – SAVVY

+0

oui, "private static final int EXT_STORAGE_PERMISSION_REQ_CODE = 3;" Mais je ne sais rien sur les chiffres, qu'est-ce que le numéro 1, 2, 3, 4? – SilverBlue

0

Copiez la classe donnée dans votre projet.

package com.betaiit.helper; 

import android.Manifest; 
import android.annotation.TargetApi; 
import android.app.Activity; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.pm.PackageManager; 
import android.os.Build; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AlertDialog; 

/** 
* Created by Sathish Gadde on 08/05/17. 
*/ 
public class Utility { 

public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123; 
public static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 124; 



/** 
* Check READ_EXTERNAL_STORAGE Permission 
*/ 
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
public static boolean checkPermission_ReadExternalStorage(final Context context) { 
    int currentAPIVersion = Build.VERSION.SDK_INT; 
    if (currentAPIVersion >= Build.VERSION_CODES.M) { 
     if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
      if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) { 
       AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context); 
       alertBuilder.setCancelable(true); 
       alertBuilder.setTitle("Permission necessary"); 
       alertBuilder.setMessage("External storage permission is necessary"); 
       alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
        public void onClick(DialogInterface dialog, int which) { 
         ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); 
        } 
       }); 
       AlertDialog alert = alertBuilder.create(); 
       alert.show(); 

      } else { 
       ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); 
      } 
      return false; 
     } else { 
      return true; 
     } 
    } else { 
     return true; 
    } 

} 

/** 
* Check WRITE_EXTERNAL_STORAGE Permission 
*/ 
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
public static boolean checkPermission_WriteExternalStorage(final Context context) { 
    int currentAPIVersion = Build.VERSION.SDK_INT; 
    if (currentAPIVersion >= Build.VERSION_CODES.M) { 
     if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
      if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 
       AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context); 
       alertBuilder.setCancelable(true); 
       alertBuilder.setTitle("Permission necessary"); 
       alertBuilder.setMessage("External storage permission is necessary"); 
       alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
        public void onClick(DialogInterface dialog, int which) { 
         ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); 
        } 
       }); 
       AlertDialog alert = alertBuilder.create(); 
       alert.show(); 

      } else { 
       ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); 
      } 
      return false; 
     } else { 
      return true; 
     } 
    } else { 
     return true; 
    } 

} 

} 

Vérifiez maintenant la permission de votre activité:

Utility.checkPermission_ReadExternalStorage(context); 
    Utility.checkPermission_WriteExternalStorage(context); 

Vous pouvez maintenant vérifier permisson de votre Fragment:

Utility.checkPermission_ReadExternalStorage(getActivity()); 
    Utility.checkPermission_WriteExternalStorage(getActivity()); 
+0

Wow, cela pourrait être correct. Mais peut-être, juste peut-être, ajouter une petite explication à votre réponse, pourquoi vous devez faire certaines choses dans votre code et comment cela fonctionne. Atm, vous donnez du poisson, au lieu d'enseigner à pêcher. – JacksOnF1re

0

Je suppose que vous voulez comme ceci:

enter image description here

je veux donner une solution simple:

Ajouter la bibliothèque dans vos dépendances de fichiers Gradle {} section:

compile 'com.karumi:dexter:4.1.0' 

Personnalisez quelques changements dans votre code et que les changements sont très faciles à comprendre. Si vous avez des questions, n'hésitez pas à me demander.

private void checkwriteStoragePermission() 
{ 

    new android.support.v7.app.AlertDialog.Builder(this) 
      .setTitle(R.string.permissions_title) 
      .setMessage(R.string.read_ext_permissions_message) 
      .setPositiveButton(R.string.btn_continue, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 

        Dexter.withActivity(MultipleBranchesActivity.this) 
          .withPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) 
          .withListener(new PermissionListener() { 
           @Override 
           public void onPermissionGranted(PermissionGrantedResponse response) { 
            Toast.makeText(MultipleBranchesActivity.this, "Permisson Granted", Toast.LENGTH_SHORT).show(); 
           } 

           @Override 
           public void onPermissionDenied(PermissionDeniedResponse response) { 
            Toast.makeText(MultipleBranchesActivity.this, "Permisson Denied", Toast.LENGTH_SHORT).show(); 

           } 

           @Override 
           public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */} 
          }).check(); 

       } 
      }) 
      .setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 

       } 
      }) 
      .setCancelable(false) 
      .show(); 


} 

et appelez votre méthode.