AndroidregisterForActivityResult启动时没有提示权限请求
创始人
2024-10-09 18:01:51
0

在registerForActivityResult方法中使用ActivityResultLauncher对象时,应该先提前请求权限,并在权限被授予时才启动ActivityResultLauncher的launch方法。以下是示例代码:

// 在Activity中定义权限请求代码 private static final int REQUEST_CODE_PERMISSIONS = 100;

// 在Activity中定义需要请求的权限 private final String[] REQUIRED_PERMISSIONS = new String[]{ Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE };

// 在Activity中定义ActivityResultLauncher对象 private final ActivityResultLauncher photoLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { // 处理ActivityResult返回的结果 } );

// 在Activity中定义权限请求Launcher对象 private final ActivityResultLauncher permissionLauncher = registerForActivityResult( new ActivityResultContracts.RequestMultiplePermissions(), permissions -> { if (permissions.containsValue(false)) { // 权限被拒绝 } else { // 权限被授予,启动photoLauncher的launch方法 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); photoLauncher.launch(intent); } } );

// 在需要启动photoLauncher的地方调用以下方法,先请求权限 if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // 权限已经被授予,直接启动photoLauncher的launch方法 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); photoLauncher.launch(intent); } else { // 权限还没被授予,请求权限 permissionLauncher.launch(REQUIRED_PERMISSIONS); }

相关内容

热门资讯

近日!hh poker插件下载... 近日!hh poker插件下载,微乐广西麻辣辅助器,推荐脚本(有挂透明挂)-哔哩哔哩hh poker...
脚本推荐!wepoker怎么设... 脚本推荐!wepoker怎么设置盖牌,wepokerplus外g挂(透视)确实有app(哔哩哔哩)1...
出现新变化!竞技联盟透视插件,... 出现新变化!竞技联盟透视插件,微乐贵阳足鸡麻将开挂,详情脚本(真的有挂)-哔哩哔哩1、下载好竞技联盟...
脚本解密!wpk德州局透视,w... 脚本解密!wpk德州局透视,wpk模拟器多开(透视)果然是有器(哔哩哔哩)1、每一步都需要思考,不同...
透视演示!htx矩阵wepok... 透视演示!htx矩阵wepoker辅助,WePoKer钻石一贯真的有教程,八分钟教程(有挂猫腻)1、...
最终!线上德州的辅助器是什么,... 最终!线上德州的辅助器是什么,微乐广西自建房免费黑科技下载,透视透视(有挂猫腻)-哔哩哔哩1、进入游...
透视操作!hhpoker可以控... 透视操作!hhpoker可以控制吗,HHpoker德州竟然真的是有安装,2分钟教程(详细教程)1、操...
围绕透视问题!德州hhpoke... 围绕透视问题!德州hhpoker是真的吗,小程序微乐贵阳捉鸡麻将挂,详情透视(有挂猫腻)-哔哩哔哩所...
透视必备!aapoker透视方... 透视必备!aapoker透视方法,aapoker公共底牌(透视)本来是真的教程(哔哩哔哩)1、很好的...
透视辅助!wejoker开挂,... 透视辅助!wejoker开挂,微乐小程序游戏辅助器脚本官网,分享脚本(有挂规律)-哔哩哔哩透视辅助!...