我想创建一个自定义通知。所以我想改变灯光和色调。我使用NotificationCompat.Builder
现在我想通过setLights()
改变灯光;工作很好。但是我想设置onMS
和offMS
的默认值。我没有找到有关的资料。
谁能帮我找到默认值?下面是相关文档:http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setLights(int, int, int)
查看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可能有不同的值。例如,对于config_defaultNotificationLedOff
, mine返回5000
。所以你可能想在运行时取回它们:
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 level 8+)上存在
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()
中的默认onMS
和offMS
不能:(
你应该可以这样做:
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);