2013-02-27 41 views
8

我想創建一個自定義通知。所以我想改變燈光和音調。 我使用NotificationCompat.Builder通知setLights()默認值?

現在我想通過setLights()改變燈光; 工作正常。但我想設置默認值onMSoffMS。我沒有找到有關這方面的信息。

任何人都可以幫助我找到默認值嗎? 下面是該文檔:http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setLights(int, int, int)

回答

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); 
+0

對不起,但這是混淆。 U使用Notification notf = .. Builder。那不運行。我也無法調用build()。 (我用作最小的SDK 8)。所以我必須調用builder.getNotification();.但是這也不會運行... – StefMa 2013-02-27 11:08:36

+0

@StefanM。我用我的示例項目中的實際代碼更新了我的答案。 'NotificationCompat.Builder.build'可以從API級別3獲得。 – 2013-02-27 11:21:02

+0

只有更多的困惑... build()is'nt available說Eclipse!和setIcon太... – StefMa 2013-02-27 11:33:05

1

@Aleks摹 沒有幫助。我有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() :(

+0

build()在API 16中作爲getNotification()的同義詞添加,但它們做同樣的事情。 – dsandler 2013-02-28 19:14:06

6

默認onMSoffMSAndroid 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,這些屬性保證EXIS t在Android 2.2+(API等級8+)上。