How to avoid writing duplicate boilerplate code for requesting permissions?

前端 未结 2 601
无人共我
无人共我 2021-02-06 02:53

I\'m updating my app to be Android 6-compatible. The permission model is not very complex in theory, but now I\'m in the process of implementing it and I find myself writing the

2条回答
  •  孤独总比滥情好
    2021-02-06 03:23

    This is what I used for managing permissions in my app.

    Parent Class for Permission

        public class PermissionManager extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
        private static final int REQUEST_CODE = 200;
        private Activity context;
    
        private String[] permissions;
        private ArrayList grantedPermissions = new ArrayList<>();
        private RequestedPermissionResultCallBack callBack;
    
        @Override
        public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
            super.onCreate(savedInstanceState, persistentState);
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            setContentView(R.layout.actvity_permission);
            checkForRequiredPermission(getIntent().getStringArrayExtra(getString(R.string.permission)));
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    
            switch (requestCode) {
    
                case REQUEST_CODE:
    
                    checkForGrantedPermissions(permissions, grantResults);
                    break;
            }
    
        }
    
    
        @TargetApi(Build.VERSION_CODES.M)
        private void checkForRequiredPermission(String[] permissions) {
            ArrayList requiredPermissionList = new ArrayList<>();
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                    requiredPermissionList.add(permission);
                } else {
                    grantedPermissions.add(permission);
                }
            }
    
            if (requiredPermissionList.size() > 0) {
                (this).requestPermissions(requiredPermissionList.toArray(new String[requiredPermissionList.size()]), REQUEST_CODE);
            } else {
    
                setResult(grantedPermissions);
            }
    
        }
    
    
        public void checkForGrantedPermissions(String[] permissions, int[] grantResults) {
    
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    grantedPermissions.add(permissions[i]);
                }
    
            }
            setResult(grantedPermissions);
    
        }
    
        private void setResult(ArrayList grantedPermissions) {
            Intent intent = new Intent();
            intent.putStringArrayListExtra(getString(R.string.granted_permission), grantedPermissions);
            setResult(Activity.RESULT_OK, intent);
            this.finish();
        }
    }
    

    When you want to check for permission call this class like this

    private void checkForPermissions() {
            Intent intent = new Intent(this, PermissionManager.class);
            intent.putExtra(getString(R.string.permission), permission);
            startActivityForResult(intent, AppConstants.PERMSION_REQUEST_CODE);
        }
    

    Here permission is an array of permission you want to ask for something like this

        private String permission[] = new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_SMS};
    

    and this is the code for onActivityResult

    case AppConstants.PERMSION_REQUEST_CODE:
                        ArrayList grantedPermissionList = data.getStringArrayListExtra(getString(R.string.granted_permission));
    
                        if (grantedPermissionList != null && grantedPermissionList.size() > 0 && grantedPermissionList.contains(permission[0])) {
                            createRequest();
    
                        } else {
                            showSettingsDialog(getString(R.string.permission_required));
                        }
    
                        break;
    

提交回复
热议问题