Add to definition constant
String[]
permiss={Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};
To be used on click events
// Dynamic permissions Cameraperssion(); private void Cameraperssion() {
PermissionsUtils.getInstance().chekPermissions(MyDataActivity.this, permiss,
new PermissionsUtils.IPermissionsResult() { @Override public void
passPermissons() { } @Override public void forbitPermissons() { } }); }
PermissionsUtils
public class PermissionsUtils { private final int mRequestCode = 100;// Permission request code
public static boolean showSystemSetting = true; private PermissionsUtils() { }
private static PermissionsUtils permissionsUtils; private IPermissionsResult
mPermissionsResult; public static PermissionsUtils getInstance() { if
(permissionsUtils == null) { permissionsUtils = new PermissionsUtils(); }
return permissionsUtils; } public void chekPermissions(Activity context,
String[] permissions, @NonNull IPermissionsResult permissionsResult) {
mPermissionsResult = permissionsResult; if (Build.VERSION.SDK_INT < 23)
{//6.0 Use dynamic permissions only permissionsResult.passPermissons(); return; }
// Create a mPermissionList, Determine which permissions are not granted one by one , Unauthorized permissions are stored in mPerrrmissionList in List<String>
mPermissionList = new ArrayList<>(); // Judge whether the permission you want has passed one by one for (int i = 0; i <
permissions.length; i++) { if (ContextCompat.checkSelfPermission(context,
permissions[i]) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(permissions[i]);// Add permissions that have not been granted } } // Application authority if
(mPermissionList.size() > 0) {// Permission failed , Need to apply
ActivityCompat.requestPermissions(context, permissions, mRequestCode); } else {
// All permissions have been approved , You can do what you want to do permissionsResult.passPermissons(); return; } }
// Method of callback after requesting permission // parameter : requestCode It is our own defined permission request code // parameter : permissions Is the array of permission names we requested
// parameter : grantResults
It is an array of identities of whether we allow permissions after the page pops up , The length of the array corresponds to the length of the array of permission names , Array data 0 Indicates permission ,-1 It means that we click the forbidden permission public void
onRequestPermissionsResult(Activity context, int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) { boolean hasPermissionDismiss =
false;// Permission failed if (mRequestCode == requestCode) { for (int i = 0; i <
grantResults.length; i++) { if (grantResults[i] == -1) { hasPermissionDismiss =
true; } } // If you have permission, it is not allowed if (hasPermissionDismiss) { if (showSystemSetting) {
showSystemPermissionsSettingDialog(context);// Jump to system settings permission page , Or close the page directly , He was not allowed to continue his visit }
else { mPermissionsResult.forbitPermissons(); } } else { // All permissions passed , You can proceed to the next step ...
mPermissionsResult.passPermissons(); } } } /** * Display dialog box when permission is no longer prompted */ AlertDialog
mPermissionDialog; private void showSystemPermissionsSettingDialog(final
Activity context) { final String mPackName = context.getPackageName(); if
(mPermissionDialog == null) { mPermissionDialog = new
AlertDialog.Builder(context) .setMessage(" Permission disabled , Please grant manually ")
.setPositiveButton(" set up ", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
cancelPermissionDialog(); Uri packageURI = Uri.parse("package:" + mPackName);
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
packageURI); context.startActivity(intent); context.finish(); } })
.setNegativeButton(" cancel ", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) { // Close the page or do something else
cancelPermissionDialog(); //mContext.finish();
mPermissionsResult.forbitPermissons(); } }) .create(); }
mPermissionDialog.show(); } // close dialog boxes private void cancelPermissionDialog() { if
(mPermissionDialog != null) { mPermissionDialog.cancel(); mPermissionDialog =
null; } } public interface IPermissionsResult { void passPermissons(); void
forbitPermissons(); } }

Technology