Android11设备不显示徽章计数
创始人
2024-08-20 12:00:04
0

在Android 11上显示徽章计数需要使用Notification Badging API。以下是示例代码:

//获取应用程序上下文 Context context = getApplicationContext();

//构建通知管理器 NotificationManager notificationManager = context.getSystemService(NotificationManager.class);

//检查应用是否被授予允许管理通知 if (notificationManager.areNotificationsEnabled()) {

//应用程序标识符
String packageName = context.getPackageName();

//构建查询条件
NotificationQuery query = new NotificationQuery.Builder()
        .setPackage(packageName)
        .build();

//执行查询
CompletableFuture> future = notificationManager
        .queryNotifications(query);

//查询结果回调
future.thenAccept(statusBarNotifications -> {

    //计算总数
    int total = statusBarNotifications.size();

    //显示徽章计数
    ShortcutBadger.applyCount(context, total);

});

}

需要注意的是,在Android 11上,动态设置应用程序图标的徽章计数需要用户授予特定权限。可以通过以下代码请求权限:

NotificationManager notificationManager = getSystemService(NotificationManager.class);

//检查当前是否需要请求权限 if (notificationManager.isNotificationPolicyAccessGranted()) { //已授权,执行徽章计数更新 } else { //请求授予权限 Intent intent = new Intent( android.provider.Settings .ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS); startActivity(intent); }

相关内容

热门资讯

实测发现!约局吧辅助器,哈糖大... 实测发现!约局吧辅助器,哈糖大菠萝万能挂,教你教程(有挂攻略);《WPK辅助透视》‌:支持手机实时对...
一起来讨论!wepoker透视... 一起来讨论!wepoker透视底牌,wepoker免费辅助器,安装教程(有挂技巧)准备好在wepok...
最新技巧!pokemmo脚本辅... 最新技巧!pokemmo脚本辅助下载,hhpoker软件安装包,必备教程(有挂透明)准备好在poke...
玩家必看教程!wepoker透... 玩家必看教程!wepoker透视脚本免费下载pc,拱趴大菠萝机器人,AI教程(有挂软件);建议优先通...
来一盘!we poker辅助器... 来一盘!we poker辅助器v3.3,aapoker透视脚本入口,力荐教程(有挂透明);小薇(透视...
推荐十款!hhpoker免费透... 推荐十款!hhpoker免费透视脚本,wepoker黑侠辅助器,教你教程(有挂透明)科技教程也叫必备...
玩家交流!aapoker怎么拿... 玩家交流!aapoker怎么拿好牌,hh poker软件,扑克教程(有挂神器);原来确实真的有挂(需...
玩家必备科普!wepoker安... 玩家必备科普!wepoker安装教程,wepoker轻量版透视,攻略方法(有挂软件);实战中需综合运...
分享认知!hhpoker透视脚... 分享认知!hhpoker透视脚本,德州透视脚本,攻略方法(有挂攻略);玩家必备必赢加哟《136704...
总算清楚!德普之星怎么开辅助,... 总算清楚!德普之星怎么开辅助,wpk刷入池率脚本,2025教程(有挂技巧)科技教程也叫必备教程,这是...