In Marshmallow, how do I call two or more permission dialogs one by one, like Hangouts?

I process my application code to work on Marshmallow devices, I control its permissions dialog to display in the necessary places.

This scenario is currently supported where it requires two permissions (Location and storage) , and I want to set one by one how Hangout does it. Could not find how it is configured, any solution?

enter image description here enter image description here

Here is the code I'm processing for one permission:

case REQUEST_CODE_WRITE_EXTERNAL_STORAGE: { if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { /Permission is granted Toast.makeText(this, "SDK >= 23 & permission Granted ", Toast.LENGTH_SHORT).show(); return true; } else { //Permission is revoked ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_WRITE_EXTERNAL_STORAGE); return false; } } 

And in onRequestPermissionsResult() :

 case REQUEST_CODE_WRITE_EXTERNAL_STORAGE: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. Log.e("PMS", "granted"); Toast.makeText(this, "SDK >= 23 & permission Granted ", Toast.LENGTH_SHORT).show(); } else { Log.e("PMS", "Not Granted"); // permission denied, boo! Disable the // functionality that depends on this permission. int checkStatus = getPermissionStatus(Manifest.permission.WRITE_EXTERNAL_STORAGE); if (checkStatus == 3) { Toast.makeText(this, "SDK >= 23 & permission Denied ", Toast.LENGTH_SHORT).show(); } else if (checkStatus == 4) { Toast.makeText(this, "SDK >= 23 & permission Blocked ", Toast.LENGTH_SHORT).show(); } } return; } 
+5
source share
4 answers

To request multiple permissions one by one, you need to add all permissions to the second String[] parameter of ActivityCompat.requestPermissions . Like this:

 ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE_WRITE_EXTERNAL_STORAGE); 

Let me know if this helps.

+2
source

Check fooobar.com/questions/64984 / ... for a detailed answer. I added the answer WRITE_EXTERNAL_STORAGE to @nicks.

 public static final int REQUEST_MULTIPLE_PERMISSIONS_ID = 456; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(checkAndRequestPermissions()) { // TODO - all permissions granted already } } private boolean checkAndRequestPermissions() { int permissionSendMessage = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION); List<String> listPermissionsNeeded = new ArrayList<>(); if (locationPermission != PackageManager.PERMISSION_GRANTED) { listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION); } if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) { listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); } if (!listPermissionsNeeded.isEmpty()) { ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_MULTIPLE_PERMISSIONS_ID); return false; } return true; } 
+1
source

I use permission for this:

 import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.util.Log; /** * Permission Utils */ public final class PermissionUtils { public static final String PER_READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE; public static final String PER_GET_ACCOUNTS = Manifest.permission.GET_ACCOUNTS; public static final String PER_WAKE_LOCK = Manifest.permission.WAKE_LOCK; public static final String FEEDBACK_PERMISSION_REQUEST_CODE = 1001; private PermissionUtils() { } public static boolean checkPermissions(Context context, String... permissions) { for (String permission : permissions) { if (!checkPermission(context, permission)) { return false; } } return true; } public static boolean AllPermissionsGranted(Context context) { return IsAccountsInfoGranted(context) && IsDeviceInfoGranted(context); } public static boolean checkPermission(Context context, String permission) { return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; } //define functions for each permission public static boolean IsWakeLockGranted(Context context) { return checkPermission(context, PER_WAKE_LOCK); } public static boolean IsAccountsInfoGranted(Context context) { return checkPermission(context, PER_GET_ACCOUNTS); } public static boolean IsDeviceInfoGranted(Context context) { return checkPermission(context, PER_READ_PHONE_STATE); } public static boolean PermissionsGranted(@NonNull int[] permissionResults) { if (permissionResults.length > 0) { for (int permission : permissionResults) { if (permission != PackageManager.PERMISSION_GRANTED) return false; } return true; } else return false; } public static void RequestPermissions(Activity activity, int permissionId, String... permissions) { ActivityCompat.requestPermissions(activity, permissions, permissionId); } } 

use this code to request permissions anywhere:

 String[] permissions = new String[]{PermissionUtils.PER_GET_ACCOUNTS, PermissionUtils.PER_READ_PHONE_STATE}; PermissionUtils.RequestPermissions(Act_Launch.this, PermissionUtils.FEEDBACK_PERMISSION_REQUEST_CODE, permissions); 

And this method in action to handle permission or application failure

  /*************************************** * RequestPermissionResult ***************************************/ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case PermissionUtils.PERMISSION_REQUEST_CODE: if (PermissionUtils.PermissionsGranted(grantResults)) { ShowRegistrationView(); } else { //Do Whatever you want } break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } 
0
source

You need to send multiple permissions simultaneously in the array. I recommend using the PermissionHelper or PermissionsDispatcher libraries. Then you can simply query it like this:

 permissionHelper .setForceAccepting(false)// true if you had like force reshowing the permission dialog on Deny (not recommended) .request(MULTIPLE_PERMISSIONS_ARRAY); 

Hope this helps!

0
source

Source: https://habr.com/ru/post/1246609/


All Articles