1

我在我的 Android 10 应用中使用气泡功能。所以我需要请求许可才能启用气泡功能。如果用户同意该权限,则需要通过启用它的确切路径。我如何做到这一点。提前致谢。

4

1 回答 1

1

我可以为 Android 11 回答这个问题。

第一种方法将检查气泡是否启用。

/** Returns true if bubbles are enabled for this app */
private boolean canDisplayBubbles() {
  if (Build.VERSION.SDK_INT < MIN_SDK_BUBBLES) {
    return false;
  }

  // NotificationManager#areBubblesAllowed does not check if bubbles have been globally disabled,
  // so we use this check as well.
  boolean bubblesEnabledGlobally;
  try {
    bubblesEnabledGlobally = Settings.Global.getInt(getContentResolver(), "notification_bubbles") == 1;
  } catch (Settings.SettingNotFoundException e) {
    // If we're not able to read the system setting, just assume the best case.
    bubblesEnabledGlobally = true;
  }

  NotificationManager notificationManager = getSystemService(NotificationManager.class);
  return bubblesEnabledGlobally && notificationManager.areBubblesAllowed();
}

第二种方法将启动一个设置页面,要求用户授予您的应用显示气泡的权限。

/** Launches a settings page requesting the user to enable Bubbles for this app */
private void requestBubblePermissions() {
  startActivityForResult(
    new Intent(Settings.ACTION_APP_NOTIFICATION_BUBBLE_SETTINGS)
      .putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName()),
    REQUEST_CODE_BUBBLES_PERMISSION);
}
于 2020-10-20T14:31:40.277 回答