如何在Android 13上获得POST_NOTIFICATION权限请求的结果?

avwztpqn  于 2022-11-27  发布在  Android
关注(0)|答案(2)|浏览(291)

我正在使用具有前台服务的第三方屏幕录制库,当屏幕录制开始时,系统会在Android 13上自动显示新的后通知权限对话框。是否有办法注册侦听器以获取有关是否授予该权限的数据?
我尝试在屏幕录制开始之前手动请求发布通知权限,但请求后未显示权限对话框。

requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)

这一切都只发生在Android 13.

yshpjwxd

yshpjwxd1#

这样请求:

private void request_notification_api13_permission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        if (this.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.POST_NOTIFICATIONS}, 22);
        }
    }
}

检查结果如下:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == 22) {
        if (grantResults.length > 0)
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

               // permission granted, perform required code

            } else {
                // not granted
            }
    }
}

你可以把22号改成任何你想要的号码。

p8h8hvxi

p8h8hvxi2#

  • 在应用程序的清单文件中声明权限
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
  • 向用户请求权限
if (Build.VERSION.SDK_INT >= 33) {
if (ContextCompat.checkSelfPermission(activity, POST_NOTIFICATIONS) != PermissionChecker.PERMISSION_GRANTED) {
       requestPermissionLauncher.launch(POST_NOTIFICATIONS);

            } else {
                 //permission already granted

              }
          }
  • 注册权限回调,该回调处理用户对系统权限对话框的响应。将返回值(ActivityResultLauncher的示例)保存为示例变量。
private ActivityResultLauncher<String> requestPermissionLauncher =
          registerForActivityResult(new RequestPermission(), 
                   isGranted -> {
              if (isGranted) {
                  // Permission is granted. Continue the action or workflow in your 
                      app.
              } else {
                    // Explain to the user that the feature is unavailable because the
      // feature requires a permission that the user has denied. At the
      // same time, respect the user's decision. Don't link to system
      // settings in an effort to convince the user to change their
      // decision.
              }
          });

希望它会有帮助!!

相关问题