Android6.0网络权限管理

Android 6.0 Marshmallow(棉花糖)是目前谷歌公布的最新版本的操作系统,其中一项改进是针对程序调用权限控制又进一步升级。
在之前的Android系统中,用户在安装应用时会收到一堆权限申请,比如允许应用访问相册、位置、通讯录等等。但是用户一般都会忽视这些直接全盘接受安装。
到了Android 5.0时代,用户可以在设置选项中对每个授权许可进行开启/关闭,比如你不希望你的短信应用获取你的地理信息,那么你可以在新设置页面中关闭这一选项。

而在Android 6.0 Marshmallow中,用户在安装应用时并不需要受到这些提示的骚扰,只有在首次打开、使用过程中实际用到哪些权限才会向收到申请授权的提醒。这一点和现在的iOS非常相像。而且开发者还可以自定义网络权限提示框。

先判断是否已经授权permissionlist,在没有授权的里面检查被用户勾选不再提醒的权限permissionNeed

final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
  
private void insertDummyContactWrapper() {
    List permissionsNeeded = new ArrayList();
  
    final List permissionsList = new ArrayList();
    if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
        permissionsNeeded.add("GPS");
    if (!addPermission(permissionsList, Manifest.permission.READ_CONTACTS))
        permissionsNeeded.add("Read Contacts");
    if (!addPermission(permissionsList, Manifest.permission.WRITE_CONTACTS))
        permissionsNeeded.add("Write Contacts");
  
    if (permissionsList.size() > 0) {
        if (permissionsNeeded.size() > 0) {
            // Need Rationale
            String message = "You need to grant access to " + permissionsNeeded.get(0);
            for (int i = 1; i < permissionsNeeded.size(); i++)
                message = message + ", " + permissionsNeeded.get(i);
            showMessageOKCancel(message,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                                    REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
                        }
                    });
            return;
        }
        requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
        return;
    }
  
    insertDummyContact();
}
  
private boolean addPermission(List permissionsList, String permission) {
    if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
        permissionsList.add(permission);
        // Check for Rationale Option
        if (!shouldShowRequestPermissionRationale(permission))
            return false;
    }
    return true;
}

用户处理权限后回调activity中的onRequestPermissionResult方法

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS:
            {
            Map perms = new HashMap();
            // Initial
            perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
            perms.put(Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED);
            perms.put(Manifest.permission.WRITE_CONTACTS, 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.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    && perms.get(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED
                    && perms.get(Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
                // All Permissions Granted
                insertDummyContact();
            } else {
                // Permission Denied
                Toast.makeText(MainActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
                        .show();
            }
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}



你可能感兴趣的:(Android6.0)