1. 程式人生 > >Android6.0網路許可權管理

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<String> permissionsNeeded = new ArrayList<String>();
  
    final List<String> permissionsList = new ArrayList<String>();
    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<String> 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<String, Integer> perms = new HashMap<String, Integer>();
            // 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);
    }
}