更新:在Android 3.0之前,我正在修改原始帖子以实现兼容性。
我试图创建一个简单的通知,除了光之外的一切都很好。当通知触发时,屏幕关闭。使用这种不推荐的代码声音和震动在Android 4.0(Galaxy Nexus)和Android 2.3(HTC EVO)上起作用。
>在2.3 HTC EVO上,灯也可以工作。
>在4.0 Galaxy Nexus上,灯不起作用。
Notification notification =
new Notification(R.drawable.ic_launcher, "My Ticker!",System.currentTimeMillis());
notification.setLatestEventInfo(context, "My Title", "My Message", pendingIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
//notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
我也尝试了不是v4兼容性库的一部分的新API,所以我只能在Galaxy Nexus上测试。振动和声音再次起作用,但不起作用。
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("My Ticker")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
.setLights(0xff00ff00, 300, 100)
.setContentTitle("My Title 1")
.setContentText("My Text 1");
Notification notification = builder.getNotification();
我现在已经在两款Galaxy Nexus手机上进行了测试,而这两款手机都没有。当我运行测试时,屏幕关闭,手机上的所有其他应用程序都会触发灯,而不会出现问题。