简体   繁体   English

如何在我的应用中请求 Bubble 的许可?

[英]How to ask permission for Bubble in my app?

I am using bubble feature in my app for Android 10. So I need to ask permission to enable Bubble feature.我在我的应用程序中为 Android 10 使用气泡功能。所以我需要询问启用气泡功能的权限。 If users agree to the permission, then need to go through the exact path of enabling it.如果用户同意该权限,则需要通过启用它的确切路径 go 。 How do I achieve that.我如何做到这一点。 Thanks in advance.提前致谢。

I can answer this for Android 11.我可以为 Android 11 回答这个问题。

This first method will check if Bubbles are enabled or not.第一种方法将检查气泡是否启用。

/** 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();
}

This second method will launch a settings page asking the user to give permission for your app to display Bubbles.第二种方法将启动一个设置页面,要求用户授予您的应用显示气泡的权限。

/** 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM