如何获得LED灯通知?

Cam*_*ide 22 android

更新:我正在重新编写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;
    
    Run Code Online (Sandbox Code Playgroud)

我还尝试了不属于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();
Run Code Online (Sandbox Code Playgroud)

我现在已经在两款Galaxy Nexus手机上进行了测试,并且这两款手机都没有.当我运行测试时屏幕关闭,手机上的所有其他应用程序触发灯光没有问题.

Dra*_*ris 9

如果使用setDefaults(DEFAULT_LIGHTS)或setDefaults(DEFAULT_ALL),它将忽略FLAG_SHOW_LIGHTS以及对setLights()的任何调用.我发现在我的手机上,默认设置不会点亮任何东西.所以不要使用DEFAULT_ALL或DEFAULT_LIGHTS,并在必要时使用RGB颜色......尽管0xff00ff00应该可以在所有情况下使用.

  • 没有这个灯:.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE).setLights(0xff00ff00,300,100) (2认同)

raj*_*raj 7

当设备关闭时(未关闭电源:P),LED工作.要测试编写一个简单的应用程序以创建延迟通知,并在延迟期间关闭手机并等待看到LED闪烁.


bip*_*pin 6

今天碰到了这个......如果它仍然有用......只有某些优先级的通知 - HIGH,MAX才能使LED发光,而优先级较低的那些通知则不能.添加这个应该使LED发光......

builder.setPriority(Notification.PRIORITY_MAX)

  • DEFAULT优先级也会导致LED发光. (2认同)

Mar*_*fer 5

4懒人:

private void testNotification() {
    Notification.Builder builder = new Notification.Builder(this);
    builder.setSmallIcon(R.drawable.ic_launcher)
        .setPriority(Notification.PRIORITY_HIGH)
        .setOngoing(true);
    builder.setLights(0xff00ff00, 300, 100);
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(1, builder.build());
}
Run Code Online (Sandbox Code Playgroud)