如何启用android';s状态指示灯或更改其颜色?

如何启用android';s状态指示灯或更改其颜色?,android,Android,我找过了,但我找不到 有一些答案,但它们不适用于Android 8.0或更高版本 我在阿里巴巴和WhatsApp中看到了这个特性 如果可以的话,帮帮忙!这也可以帮助其他人 我的代码: NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >

我找过了,但我找不到

有一些答案,但它们不适用于Android 8.0或更高版本

我在阿里巴巴和WhatsApp中看到了这个特性

如果可以的话,帮帮忙!这也可以帮助其他人

我的代码:

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(getString(R.string.default_notification_channel_id), remoteMessage.getData().get("title"), NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(true);
            channel.setLightColor(0xff0000);
            channel.shouldShowLights();
            channel.canShowBadge();
            notificationManager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "0")
            .setSmallIcon(R.drawable.ic_notification)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_notification))
            .setContentTitle(remoteMessage.getData().get("title"))
            .setContentText(remoteMessage.getData().get("message"))
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setVibrate(new long[] {0, 1000, 200,1000 })
            .setChannelId(getString(R.string.default_notification_channel_id))
            .setLights(Color.RED, 100 , 100)
            .setAutoCancel(true);


        if (notificationManager != null) {
            Notification notification = notificationBuilder.build();
            notification.ledARGB = 0xFFff0000;
            notification.flags = Notification.FLAG_SHOW_LIGHTS;
            notification.ledOnMS = 100;
            notification.ledOffMS = 100;
            notificationManager.notify(new Random().nextInt(60000), notification);
        }

启用通道中的灯光


不推荐使用的代码,因此我应该删除这些代码:

        notification.ledARGB = 0xFFff0000;
        notification.flags = Notification.FLAG_SHOW_LIGHTS;
        notification.ledOnMS = 100;
        notification.ledOffMS = 100;
并将重要性\u默认值替换为重要性\u最大值如下

NotificationChannel channel = new NotificationChannel(getString(R.string.default_notification_channel_id), 
remoteMessage.getData().get("title"), NotificationManager.IMPORTANCE_MAX);
就像@tyzj的回答和@dharms的评论一样,我用alpha颜色添加了这个:

channel.enableLights(true);
channel.setLightColor(0xffff0000)
改变

.setLights(Color.RED, 100 , 100) 


我看到的一个可能的问题是您的
频道。setLightColor()
在ARGB中不包括A。您可以尝试将其更改为
Color.RED
,因为您正在降低,或者
0xffff0000
@dharms public static final int RED=0xFFFF0000Yes,但您没有使用红色。您有
channel.setLightColor(0xff0000)
@dharms它在有或没有它的情况下仍然不能工作
.setLights(0xffff0000, 1000 , 1000)