2013-02-27 1 views
8

Ich möchte eine benutzerdefinierte Benachrichtigung erstellen. Also möchte ich die Lichter und den Ton ändern. Ich benutze dafür die NotificationCompat.Builder.Benachrichtigung setLights() Standardwert?

Jetzt möchte ich die Lichter über setLights() ändern; Funktioniert gut. Aber ich möchte den Standardwert der onMS und offMS setzen. Ich habe nichts darüber gefunden.

Kann mir jemand helfen, die Standardwerte zu finden? Hier ist die Dokumentation, dass: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setLights(int, int, int)

Antwort

1

Sie sollten in der Lage sein, dies zu tun mit:

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 

Grundsätzlich verwenden Sie nicht setLights über die Mitteilung Builder. Erstellen Sie stattdessen zuerst die Benachrichtigung - dann haben Sie Zugriff auf einzelne Felder für die Lichter.

Update: dies ist das eigentliche Kopieren/Einfügen von meinem Beispielprojekt, das und funktioniert auf Android 2.1 Fein kompiliert und verwendet blaue Farbe für 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

Sorry, aber das ist verwirrt. Benutze Notification notf = .. Builder. Das läuft nicht. Auch ich kann Build() nicht aufrufen. (Ich verwende als kleinste SDK 8). Also muss ich builder.getNotification(); aufrufen. Aber das läuft auch nicht ... – StefMa

+0

@StefanM. Ich habe meine Antwort mit dem tatsächlichen Code aus meinem Beispielprojekt aktualisiert. 'NotificationCompat.Builder.build' ist ab API-Level 3 verfügbar. –

+0

Nur mehr verwirrt ... build() ist nicht verfügbar, sagt Eclipse! Und das SetIcon auch ... – StefMa

1

@Aleks G , die nicht helfen . Ich habe das neueste Update vom compat libaray. Aber Eclipse sagen build() ist nicht verfügbar. Ich weiß nicht warum. Die Doku sagt ja und Sie ...

Dies ist meine aktuellen Code:

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

läuft perfekt. Aber nicht mit dem Standard-onMS und offMS in setLights() :(

+0

Build() wurde als ein Synonym für getNotification() in API 16 hinzugefügt, aber sie tun das Gleiche. – dsandler

6

Siehe Android source für Antwort.

<!-- 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> 

jedoch unterschiedliche ROMs könnten für diese unterschiedliche Werte haben Zum Beispiel gibt Mine 5000 für config_defaultNotificationLedOff So könnten Sie. wollen, dass sie während der Laufzeit holen:

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

nach diff werden diese Attribute zu exis garantiert t auf Android 2.2+ (API-Level 8+).