Android。通知显示在状态栏中,但不显示或发出声音。API 24。
创始人
2024-10-13 07:01:00
0

要在Android中在状态栏中显示通知但不发出声音,您可以使用以下代码示例:

// 创建通知渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    CharSequence name = "Channel Name";
    String description = "Channel Description";
    int importance = NotificationManager.IMPORTANCE_LOW;
    NotificationChannel channel = new NotificationChannel("channel_id", name, importance);
    channel.setDescription(description);
    
    // 禁用声音
    channel.setSound(null, null);
    
    // 创建通知渠道
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}

// 创建通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("Notification Title")
        .setContentText("Notification Text")
        .setPriority(NotificationCompat.PRIORITY_LOW)
        .setSound(null) // 禁用声音
        .setVibrate(new long[]{0}) // 禁用振动
        .setDefaults(NotificationCompat.DEFAULT_LIGHTS) // 禁用其他默认行为
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

// 显示通知
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, builder.build());

在上面的代码示例中,我们首先使用NotificationChannel类创建了一个通知渠道,并禁用了声音。然后,我们使用NotificationCompat.Builder创建了一个通知实例,并将其与之前创建的通道相关联。我们还禁用了通知的声音、振动和其他默认行为。

最后,我们使用NotificationManagerCompatnotify方法显示通知。

请注意,上述代码仅适用于API级别24及更高版本。对于API级别低于24的设备,您可以使用不同的方法来禁用声音,例如:

builder.setDefaults(0);

相关内容

热门资讯

2024教程!wepoker怎... 1、2024教程!wepoker怎么增加运气,pokemmo脚本辅助下载,高科技教程(有挂辅助)。2...
盘点一款!We poker辅助... 盘点一款!We poker辅助器下载,hhpoker到底可以作弊吗,透明挂教程(有挂神器)是由北京得...
玩家必看攻略!we-poker... 《玩家必看攻略!we-poker是什么软件,aapoker怎么开辅助器,插件教程(有挂技巧)》 aa...
玩家必看科普!约局吧开挂神器是... 玩家必看科普!约局吧开挂神器是真的吗,wepoker好友助力码,力荐教程(有挂技巧);《WPK辅助透...
交流学习经验!werplan辅... 自定义wepoker辅助真的假的系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅...
教程辅助!wepoker有没有... 教程辅助!wepoker有没有挂,hhpoker是正品吗,技巧教程(有挂软件);《WPK辅助透视》‌...
最新通报!wepoker怎么设... 1、最新通报!wepoker怎么设置房间,wepoker辅助真的假的,AA德州教程(有挂教程)(UU...
热点讨论!hhpoker辅助器... 热点讨论!hhpoker辅助器,hh poker透视器下载,高科技教程(有挂透明);人气非常高,ai...
重大消息!wepoker透视苹... 重大消息!wepoker透视苹果系统,wepoker怎么设置盖牌,必胜教程(有挂技巧);人气非常高,...
交流学习经验!wepoker免... 交流学习经验!wepoker免费辅助器,hhpoker免费辅助器,辅助教程(有挂攻略);人气非常高,...