通知setLights()默认值?

Ste*_*fMa 9 notifications android android-notifications

我想创建一个自定义通知.所以我想改变灯光和音调.我用它NotificationCompat.Builder来做那件事.

现在我想改变灯光通过setLights(); 工作良好.但我想设置的默认值onMSoffMS.我还没有找到相关的东西.

任何人都可以帮我找到默认值吗?这是以下文档:http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setLights(int,int,int)

Myg*_*god 7

请参阅Android源代码以获取答案:

<!-- Default color for notification LED. -->
<color name="config_defaultNotificationColor">#ffffffff</color>
<!-- Default LED on time for notification LED in milliseconds. -->
<integer name="config_defaultNotificationLedOn">500</integer>
<!-- Default LED off time for notification LED in milliseconds. -->
<integer name="config_defaultNotificationLedOff">2000</integer>
Run Code Online (Sandbox Code Playgroud)

但是,不同的ROM可能具有不同的值.例如我的返回5000config_defaultNotificationLedOff.所以你可能想在运行时获取它们:

Resources resources = context.getResources(),
          systemResources = Resources.getSystem();
notificationBuilder.setLights(
    ContextCompat.getColor(context, systemResources
        .getIdentifier("config_defaultNotificationColor", "color", "android")),
    resources.getInteger(systemResources
        .getIdentifier("config_defaultNotificationLedOn", "integer", "android")),
    resources.getInteger(systemResources
        .getIdentifier("config_defaultNotificationLedOff", "integer", "android")));
Run Code Online (Sandbox Code Playgroud)

根据差异,这些属性保证存在于Android 2.2+(API级别8+)上.


Ale*_*s G 1

您应该能够通过以下方式执行此操作:

Notifictaion notf = new Notification.Builder(this).setXXX(...).....build();
notf.ledARGB = <your color>;
notf.ledOnMS = <your value>;  //or skip this line to use default
notf.ledOffMS = <your value>; //or skip this line to use default
Run Code Online (Sandbox Code Playgroud)

基本上,不要setLights在通知生成器上使用。相反,首先构建通知 - 然后您可以访问灯光的各个字段。

更新:这是我的示例项目的实际复制/粘贴,它可以在 android 2.1 上编译并正常工作,并使用蓝色 LED:

Notification notf = new NotificationCompat.Builder(this)
    .setAutoCancel(true)
    .setTicker("This is the sample notification")
    .setSmallIcon(R.drawable.my_icon)
    .build();
notf.ledARGB = 0xff0000ff;
NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notf);
Run Code Online (Sandbox Code Playgroud)