如何启用Android的状态LED或更改其颜色?

时间:2019-04-05 15:29:51

标签: android

我搜索了一下,但是我无法弄清楚!

有一些答案,但不能在 Android 8.0 或更高版本中使用。

我在 Alibaba 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);
        }

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

已弃用的代码,因此我应该删除这些代码:

        notification.ledARGB = 0xFFff0000;
        notification.flags = Notification.FLAG_SHOW_LIGHTS;
        notification.ledOnMS = 100;
        notification.ledOffMS = 100;

IMPORTANCE_DEFAULT 替换为 IMPORTANCE_MAX ,如下所示

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) 

收件人

.setLights(0xffff0000, 1000 , 1000)