简体   繁体   中英

Android permissions 6.0 and above

I am trying to create my own class of checking request permission for Marshmallow and know that there are normal and dangerous permissions. The very basic feature of the app is to block incoming call.

public class RequestPermission {

    private String[] modifyPhoneStatePermission = {Manifest.permission.MODIFY_PHONE_STATE};
    private String[] contactsPermission = {Manifest.permission.READ_CONTACTS};
    private String[] phonePermissions = {Manifest.permission.CALL_PHONE, Manifest.permission.READ_PHONE_STATE};


    private Context context;

    public RequestPermission(Context context)
    {
       this.context = context;
    }

public boolean checkModifyPhoneStatePermission() {
    try
    {
        if (ContextCompat.checkSelfPermission(context, modifyPhoneStatePermission[0]) == PackageManager.PERMISSION_GRANTED) {

            return true;

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

     public void askModifyPhoneStatePermission() {
    try
    {
        ActivityCompat.requestPermissions((Activity) context, modifyPhoneStatePermission, REQUEST_CODE_PERMISSION);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

} 

However, when I try this on a separate class, a dialog box does not pop up. Why is this happening? Should I include this on phonePermissions array? What is the participation of MODIFY_PHONE_STATE in blocking calls?

Checkout below Code :

      private static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 10001;

                   @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

                if (platform_version >= 23) {
                    boolean permission = checkAndRequestPermissions();
                    /*if (permission)
                        Toast.makeText(this, "All permissions granted!", Toast.LENGTH_LONG).show();*/
                }

        }







        private boolean checkAndRequestPermissions() {
            int modifyPhoneStatePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.MODIFY_PHONE_STATE);
            int contactsPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS);
            int phonePermissions = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            int permissionReadExternal = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);

            List<String> listPermissionsNeeded = new ArrayList<>();
            if (modifyPhoneStatePermission != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(Manifest.permission.MODIFY_PHONE_STATE);
            }
            if (contactsPermission != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(Manifest.permission.READ_CONTACTS);
            }
            if (phonePermissions != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
            }
 if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
            return false;
        }

            return true;
        }





          @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
            Log.d(PERMISSION_TAG, "Permission callback called...");
            switch (requestCode) {
                case REQUEST_ID_MULTIPLE_PERMISSIONS: {

                    Map<String, Integer> perms = new HashMap<>();
                    // Initialize the map with both permissions

                    perms.put(Manifest.permission.MODIFY_PHONE_STATE, PackageManager.PERMISSION_GRANTED);
                    perms.put(Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED);
                    perms.put(Manifest.permission.READ_PHONE_STATE, PackageManager.PERMISSION_GRANTED);

                    // Fill with actual results from user
                    if (grantResults.length > 0) {
                        for (int i = 0; i < permissions.length; i++)
                            perms.put(permissions[i], grantResults[i]);
                        // Check for both permissions
                        if (perms.get(Manifest.permission.ACCESS_NETWORK_STATE) == PackageManager.PERMISSION_GRANTED &&
                                perms.get(Manifest.permission.ACCESS_WIFI_STATE) == PackageManager.PERMISSION_GRANTED &&
                                perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
                                perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                            Log.d(PERMISSION_TAG, "permission granted");
                            // process the normal flow
                            //else any one or both the permissions are not granted
                        } else {
                            Log.d(PERMISSION_TAG, "Some permissions are not granted ask again ");
                            //permission is denied (this is the first time, when "never ask again" is not checked) so ask again explaining the usage of permission
    //                      //shouldShowRequestPermissionRationale will return true
                            //show the dialog or snackbar saying its necessary and try again otherwise proceed with setup.
                            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_NETWORK_STATE) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                                showDialogOK("Permission required for this app",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                switch (which) {
                                                    case DialogInterface.BUTTON_POSITIVE:
                                                        checkAndRequestPermissions();
                                                        break;
                                                    case DialogInterface.BUTTON_NEGATIVE:
                                                        // proceed with logic by disabling the related features or quit the app.
                                                        checkAndRequestPermissions();
                                                        break;
                                                }
                                            }
                                        });
                            }
                            //permission is denied (and never ask again is  checked)
                            //shouldShowRequestPermissionRationale will return false
                            else {
                                //proceed with logic by disabling the related features or quit the app.

                            }
                        }
                    }
                }
            }
        }

Below code will work in your case , you can use the below check in any method you want or in onCreate()

   if (platform_version >= 23) {
                boolean permission = checkAndRequestPermissions();
                /*if (permission)
                    Toast.makeText(this, "All permissions granted!", Toast.LENGTH_LONG).show();*/
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM