【问题标题】:Notification setLights() Default Value?通知 setLights() 默认值?
【发布时间】:2013-02-13 02:42:12
【问题描述】:

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

现在我想通过setLights() 更改灯光; 工作正常。但我想设置onMSoffMS 的默认值。我还没有找到相关的信息。

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

【问题讨论】:

    标签: android notifications android-notifications


    【解决方案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);
    

    【讨论】:

    • 抱歉,这很混乱。 U 使用通知 notf =.. 生成器。那不运行。我也不能调用 build()。 (我用作最小的 SDK 8)。所以我必须调用builder.getNotification();。但这也不会运行...
    • @StefanM。我用示例项目中的实际代码更新了我的答案。 NotificationCompat.Builder.build 可从 API 级别 3 获得。
    • 只是更困惑... build() is'nt available 说 Eclipse!还有 setIcon...
    • @StefanM。抱歉,应该是setSmallIconsetLargeIconbuild 可用 - 我的 Eclipse 很乐意将它用于 API 级别 7。也许你应该更新你的兼容性库。
    【解决方案2】:

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

    这是我当前的代码:

        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());
    

    完美运行。但不适用于setLights() 中的默认onMSoffMS :(

    【讨论】:

    • build() 在 API 16 中被添加为 getNotification() 的同义词,但它们的作用相同。
    【解决方案3】:

    请参阅Android source 以获得答案:

    <!-- 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+)上存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      相关资源
      最近更新 更多