9

我想创建一个自定义通知。所以我想改变灯光和音调。我用NotificationCompat.Builder这个。

现在我想通过改变灯光setLights();工作正常。onMS但我想设置and的默认值offMS。我还没有找到关于那个的东西。

谁能帮我找到默认值?这是相关的文档:http: //developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setLights(int, int, int)

4

3 回答 3

8

请参阅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>

然而,不同的 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")));

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

于 2015-12-10T07:11:53.363 回答
2

@Aleks G 没有帮助。我有来自 compat libaray 的最新更新。但 Eclipse saybuild()是可用的。我不知道为什么。文件说是的,你...

这是我当前的代码:

    NotificationCompat.Builder notify = new NotificationCompat.Builder(context);
    notify.setLights(Color.parseColor(led), 5000, 5000);
    notify.setAutoCancel(true);
    notify.setSound(Uri.parse(tone));
    notify.setSmallIcon(R.drawable.ic_stat_kw);
    notify.setContentTitle("Ttiel");
    notify.setContentText("Text");
    Intent showIntent = new Intent(context, Activity_Login.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, showIntent, 0); 
    notify.setContentIntent(contentIntent);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notify.getNotification());

完美运行。但不是默认值onMSoffMS:(setLights()

于 2013-02-27T11:52:33.503 回答
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

基本上,不要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);
于 2013-02-27T10:34:58.167 回答