安卓6.0权限管理

/**
* 开始打电话
* @param context
* @param driver
*/
public void callPhone(Context context, DriverDetails driver) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(“tel:” + driver.getNum()));
context.startActivity(intent);

}
/**
 * 用来调用打电话
 * @param context
 * @param driver
 */
protected void callDriverPhone(Context context, DriverDetails driver) {
    if (Build.VERSION.SDK_INT >= 23) {
        int checkCallPhonePermission = ContextCompat.checkSelfPermission(context,Manifest.permission.CALL_PHONE);
        if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions((Activity) context,new String[]{Manifest.permission.CALL_PHONE},REQUEST_CODE_ASK_CALL_PHONE);
            //其它的一样,例如调用通讯录,改变请求权限为//Manifest.permission.READ_CONTACTS就好了
            return;
        }else{
            //上面已经写好的拨号方法
            callPhone(context,driver);
        }
    } else {
        //上面已经写好的拨号方法
        callPhone(context,driver);
    }

}
 @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_CALL_PHONE:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                callDirectly(mobile);
            } else {
                // Permission Denied
                Toast.makeText(MainActivity.this, "CALL_PHONE Denied", Toast.LENGTH_SHORT)
                        .show();
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

你可能感兴趣的:(6.0权限)