我有一部运行Android 8的手机,我从Android Studio开始编写Android程序。一些应用程序,如Snapchat或Facebook,在收到通知时会用它们的自定义颜色(黄色和蓝色)让我的手机亮起led灯。
我想对我的应用程序做同样的事情,我搜索了很多次,但什么都不起作用,通知出现了,但没有白灯。我检查了我的手机设置,我的应用程序被允许点亮led。
public class Notification
{
public Notification(String channelId, String channelName, Context context, String title, String body, Intent intent)
{
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = 1;
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
NotificationChannel mChannel = new
NotificationChannel(channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder nbuilder = new NotificationCompat.Builder(context, channelId)
.setLights(Color.WHITE, 500, 100) // doesn't work
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }) // doesn't work
.setContentTitle(title)
.setContentText(body);
TaskStackBuilder stackBuilder =
TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
nbuilder.setContentIntent(resultPendingIntent);
notificationManager.notify(notificationId, nbuilder.build());
}
}我试着在setContentText()之后调用setLights()和setVibrate(),也试着在notify()之前调用这些空值,但没有改变任何事情。
我在onCreate()中实例化我的类:
new Notification("channel-01", "MathWellan Notification", this, "Twitter", "Follow me on Twitter !", new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=MathWellan")));很抱歉我的英语不好,我是法国人,希望你能帮助我!提前感谢:)
发布于 2018-04-08 23:54:48
使用此行代码更改通知发光二极管颜色:your_notification.ledARGB = Color.YOUR_COLOR;
用法示例:
NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notif.cancel(1); // clear previous notification
final Notification notification = new Notification();
notification.ledARGB = Color.MAGENTA;
notification.ledOnMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notif.notify(1, notification);注意:屏幕应该在测试时锁定,因为通知只有在屏幕关闭时才会突出显示。也许,这就是你的问题。
https://stackoverflow.com/questions/49719689
复制相似问题