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是真的还是... 玩家必看教程!wpk是真的还是假的,wepoker透视脚本,透视教程(一贯确实有挂)-哔哩哔哩;亲,...
发现一款!wepokerplu... 发现一款!wepokerplus开挂,wpk官网下载链接,辅助教程(一直真的是有挂)-哔哩哔哩;1、...
揭秘几款!hhpoker可以控... 揭秘几款!hhpoker可以控制牌吗,wepoker透视脚本是什么,攻略教程!(其实是有挂的)-哔哩...
一分钟快速了解!wejoker... 一分钟快速了解!wejoker透视方法,wepoker线上大神,必备教程(果然确实有挂)-哔哩哔哩;...
一分钟了解!hhpoker免费... 一分钟了解!hhpoker免费透视脚本,pokemmo辅助器,透牌教程(一贯确实有挂)-哔哩哔哩;1...
透视规律!wepoker怎么买... 透视规律!wepoker怎么买辅助,wepoker分析,透牌教程(原来有挂)-哔哩哔哩;1.wepo...
我来向大家传授!wejoker... 我来向大家传授!wejoker开挂,竞技联盟辅助插件,分享教程(原来真的是有挂)-哔哩哔哩;竞技联盟...
重大发现!wepoker免费脚... 重大发现!wepoker免费脚本,wepoker免费辅助器,解说技巧(一直是有挂)-哔哩哔哩;1.w...
热点推荐!wepoker透视脚... 热点推荐!wepoker透视脚本安卓,德扑HHpoker有挂吗,存在挂教程(原来真的是有挂)-哔哩哔...
指导大家!wepoker的辅助... 指导大家!wepoker的辅助器,wepokerplus辅助,必备教程(其实有挂)-哔哩哔哩;1、很...