Android危险权限与动态申请

危险权限:

权限组名 权限名称
CALENDAR(日历) READ_CALENDAR
WRITE_CALENDAR
CAMERA(相机) CAMERA
CONTACTS(联系人) READ_CONTACTS
WRITE_CONTACTS
GET_ACCOUNTS
LOCATION(位置) ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
MICROPHONE(麦克风) RECORD_AUDIO
PHONE(手机) READ_PHONE_STATE
CALL_PHONE
ERAD_CALL_LOG
WRITE_CALL_LOG
ADD_VOICEMAIL
USE_SIP
PROCESS_OUTGOING_CALLS
SENSORS(传感器) BODY_SENSORS
SMS(短信) SEND_SMS
RECEIVE_SMS
READ_SMS
RECEIVE_WAP_PUSH
RECEIVE_MMS
STORAGE(存储卡) READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE

动态申请:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestPermission();//权限申请
}
private void requestPermission()
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        if (checkSelfPermission(READ_PHONE_STATE) != PERMISSION_GRANTED
                || checkSelfPermission(WRITE_EXTERNAL_STORAGE) != PERMISSION_GRANTED
        ) {
            if (shouldShowRequestPermissionRationale( READ_PHONE_STATE)
                    ||shouldShowRequestPermissionRationale( WRITE_EXTERNAL_STORAGE)
            )
            {
                requestPermissions(new String[]{READ_PHONE_STATE,WRITE_EXTERNAL_STORAGE},502);
            } else {
                ActivityCompat.requestPermissions(this,new String[]{READ_PHONE_STATE,WRITE_EXTERNAL_STORAGE},501);
            }
        }
    }
}
@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 501: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED&& grantResults[1] == PackageManager.PERMISSION_GRANTED)
                {
//                    Log.i(TAG,"onRequestPermissionsResult granted");
                } else
                {
//                    Log.i(TAG,"onRequestPermissionsResult denied");
                    showWaringDialog();
                }
                return;
            }
        }
    }

private void showWaringDialog() {
    AlertDialog dialog = new AlertDialog.Builder(this)
            .setTitle("警告:")
            .setMessage("请前往设置->应用->应用名->权限中打开相关权限,否则游戏无法正常运行!")
            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // 一般情况下如果用户不授权的话,功能是无法运行的,做退出处理
                    finish();
                }
            }).show();
}

你可能感兴趣的:(unity-android,Android,Unity,android,java,unity,permission)