android API 23中的led通知 [英] led notification in android API 23
问题描述
我想打开或闪烁android设备上的led.所以我通过下面的代码来做到这一点
I want to turn on or blink the led on android devices. So I am doing it through the code below
NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
Notification notif = new Notification();
notif.ledARGB = 0xFF0000; // #0000FF
notif.flags = Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = 100;
notif.ledOffMS = 100;
nm.notify(5, notif);
Toast.makeText(this, "RED LED SELECTED", Toast.LENGTH_LONG).show();
这有效.除非我在棉花糖6.0.1 API上进行了测试,否则它会崩溃.23.它会崩溃.有人告诉我解决方案吗?
And this works. Unless I tested it on Marshmallow 6.0.1 API: 23. It crashes. Anyone please tell me the solution?
推荐答案
请尝试以下操作:
由于无效的小图标,它引发的异常是无效通知",这在API级别23和更高级别上是必需的.另外,请始终使用NotificationCompat.Builder来构建通知,如下所示:
The exception it was throwing says Invalid Notification due to invalid small icon which is mandatory on API level 23 and above. Also, always use NotificationCompat.Builder to build a notification as under :
NotificationManagerCompat nm = NotificationManagerCompat.from(this);
NotificationCompat.Builder notif = new NotificationCompat.Builder(this);
notif.setLights(0xff0000, 100, 100);
notif.setSmallIcon(R.mipmap.ic_launcher);
notif.setContentTitle("Notification Title");
nm.notify(5, notif.build());
此外,内容标题不是强制性的,但您应使用它为用户提供有关通知的正确信息.否则,它们会很烦人.
Also, Content Title is not mandatory but you should use it to provide proper info to user about the notification. Otherwise, they are very irritating.
这篇关于android API 23中的led通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!