Android Studio:带有ARGB的通知LED上的自定义颜色不起作用

时间:2017-12-25 13:08:29

标签: android notifications android-notifications

我尝试为ARGB颜色的通知LED设置自定义颜色。

NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
        .setDefaults(0)
        .setTicker("")
        .setSmallIcon(R.drawable.maxapplogo)
        .setContentTitle("Notification")
        .setContentText("This is a notification test!")
        .setAutoCancel(true)
        .setVibrate(new long[] { 1000, 500, 1000, 500, 1000 })
        .setLights(Color.GREEN, 1000, 1000)
        .build();

/* notification.ledARGB = 0xffffee05; */

/* here I wanted to know if the int values of the color can help me */
int black = Color.BLACK;
Log.d("black color", String.valueOf(black)); // -16777216

int green = Color.GREEN;
Log.d("green color", String.valueOf(green)); // -16711936

int red = Color.RED;
Log.d("red color", String.valueOf(red)); // -65536

notification.flags = Notification.FLAG_SHOW_LIGHTS;

if(notificationManager != null) {
    notificationManager.notify(0, notification);
} else {
    Toast.makeText(getApplicationContext(), "Notification failed", Toast.LENGTH_SHORT).show();
}

如果我使用命令COLOR.GREEN或任何其他可用颜色设置颜色,它将起作用。但是使用ARGB规范它不起作用。即使使用不同的ARGB颜色,它也始终显示相同的颜色。

我在三星Galaxy S8上测试了我的应用。我没有其他智能手机可供测试。有人知道为什么这不起作用。它应该根据其他Stackoverflow帖子。

1 个答案:

答案 0 :(得分:1)

FLAG_SHOW_LIGHTS和Notification.Builder.setLights(int,int,int);自Android O(API级别26)

以来已弃用

您需要使用通知频道

NotificationChannel mChannel = new NotificationChannel(id, name, importance);

mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);