2013-02-27 34 views
8

Tôi muốn tạo thông báo tùy chỉnh. Vì vậy, tôi muốn thay đổi ánh sáng và giai điệu. Tôi sử dụng số NotificationCompat.Builder cho điều đó.Thông báo setLights() Giá trị mặc định?

Bây giờ tôi muốn thay đổi đèn qua setLights(); Hoạt động tốt. Nhưng tôi muốn đặt giá trị mặc định là onMSoffMS. Tôi đã không tìm thấy một cái gì đó về điều đó.

Ai đó có thể giúp tôi tìm các giá trị mặc định không? Đây là tài liệu cho rằng: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setLights(int, int, int)

Trả lời

1

Bạn sẽ có thể làm điều này với:

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 

Về cơ bản, không sử dụng setLights trên builder thông báo. Thay vào đó, hãy tạo thông báo trước - sau đó bạn có quyền truy cập vào các trường riêng lẻ cho đèn.

Cập nhật: đây là copy/paste thực tế từ dự án mẫu của tôi, trong đó biên dịch và hoạt động tốt trên Android 2.1 và sử dụng màu xanh cho 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

Xin lỗi nhưng điều này bị nhầm lẫn. U sử dụng Thông báo notf = .. Builder. Điều đó không chạy. Ngoài ra tôi không thể gọi build(). (Tôi sử dụng SDK 8 nhỏ nhất). Vì vậy, tôi phải gọi builder.getNotification() ;. Nhưng điều này cũng không chạy ... – StefMa

+0

@StefanM. Tôi đã cập nhật câu trả lời của mình bằng mã thực tế từ dự án mẫu của mình. 'NotificationCompat.Builder.build' có sẵn từ cấp API 3. –

+0

Chỉ còn lộn xộn hơn ... build() is'nt available Eclipse! Và setIcon cũng vậy ... – StefMa

1

@Aleks G mà không giúp . Tôi có bản cập nhật mới nhất từ ​​latay compat. Nhưng Eclipse nói build() là không thể. Tôi không biết tại sao. Các docu nói có và bạn ...

Đây là mã hiện tại của tôi:

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

chạy một cách hoàn hảo. Nhưng không phải với mặc định onMSoffMS trong setLights() :(

+0

build() được thêm vào như một từ đồng nghĩa với getNotification() trong API 16, nhưng chúng cũng làm như vậy. – dsandler

6

Xem Android source cho câu trả lời:.

<!-- 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 Tuy nhiên khác nhau có thể có giá trị khác nhau cho các Ví dụ tôi trả 5000 cho config_defaultNotificationLedOff Vì vậy, bạn có thể. muốn tìm nạp chúng khi chạy:

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

Theo diff, các thuộc tính này được đảm bảo cho exis t trên Android 2.2+ (API cấp 8+).

Các vấn đề liên quan