Android onRequestPermissionsResult弃用后的权限申请方式

private final ActivityResultLauncher<String> requestPermissionLauncher =
            registerForActivityResult(new ActivityResultContracts.RequestPermission(), result -> {
                if (result) {
                    Log.e("aonisvasd", "onActivityResult: PERMISSION GRANTED");
                } else {
                    Log.e("aonisvasd", "onActivityResult: PERMISSION DENIED");
                }
            });
			
// 使用方式
if(ContextCompat.checkSelfPermission(context,Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
    // TODO: do something...
} else {
    requestPermissionLauncher.launch(Manifest.permission.CAMERA);
}

private final ActivityResultLauncher<Intent> resultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
    if (result.getResultCode() == Activity.RESULT_OK) {

    }
});  
posted @ 2022-06-01 11:36  指切  阅读(1230)  评论(0编辑  收藏  举报