1. 程式人生 > >Android6.0許可權申請程式碼

Android6.0許可權申請程式碼

1.申請一個許可權WRITE_EXTERNAL_STORAGE

  private void requestPermision() {
        int hasWriteContactsPermission = ActivityCompat.checkSelfPermission(HotFixActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(HotFixActivity.this,new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_CODE_ASK_PERMISSIONS);
            return;
        }
        FixDexUtils.loadFixedDex(HotFixActivity.this, Environment.getExternalStorageDirectory());
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted
                    FixDexUtils.loadFixedDex(HotFixActivity.this, Environment.getExternalStorageDirectory());

                } else {
                    // Permission Denied
                    ToastUtils.showText("WRITE_EXTERNAL_STORAGE Denied");
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

2.申請多個許可權

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        this.position = arg2;
        //許可權申請
        final List<String> permissionsList = new ArrayList<String>();
        addPermission(permissionsList, Manifest.permission.RECORD_AUDIO);
        addPermission(permissionsList, Manifest.permission.CAMERA);
        if (permissionsList.size() > 0) {
            ActivityCompat.requestPermissions(HallActivity.this,permissionsList.toArray(new String[permissionsList.size()]),
                    REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
            return;
        }else {
            showUserDialog(arg2);
        }
    }

    private void showUserDialog(int arg2) {
   //...
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
                Map<String, Integer> perms = new HashMap<String, Integer>();
                // Initial
                perms.put(Manifest.permission.RECORD_AUDIO, PackageManager.PERMISSION_GRANTED);
                perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
                // Fill with results
                for (int i = 0; i < permissions.length; i++)
                    perms.put(permissions[i], grantResults[i]);
                // Check for ACCESS_FINE_LOCATION
                if (perms.get(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
                        && perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                    // All Permissions Granted
                    showUserDialog(position);
                } else {
                    // Permission Denied
                    Toast.makeText(HallActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
                            .show();
                }
            }
            break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    private boolean addPermission(List<String> permissionsList, String permission) {
        if (ActivityCompat.checkSelfPermission(HallActivity.this,permission) != PackageManager.PERMISSION_GRANTED) {
            permissionsList.add(permission);
            // Check for Rationale Option
            if (!ActivityCompat.shouldShowRequestPermissionRationale(HallActivity.this,permission))
                return false;
        }
        return true;
    }