Changing LED color for notifications

前端 未结 8 1958
囚心锁ツ
囚心锁ツ 2020-12-04 15:56

I am basically just experimenting with Android development, and a couple of days ago I came across this app called \"Go SMS Pro\", which, among other things, can set up noti

相关标签:
8条回答
  • 2020-12-04 16:12

    Try using the hex color, include an alpha value and set the defaults to 0:

    notification.defaults = 0;
    notification.ledARGB = 0xff0000ff;
    

    Also, the notification interface says this:

    public int ledARGB
    Since: API Level 1
    
    The color of the led. The hardware will do its best approximation.
    

    I'm assuming your hardware has all the colors, but it may not.

    0 讨论(0)
  • 2020-12-04 16:19

    Leds are a quite non-standard feature in android phones. If you depend in them, you will miss a good chunk of the user base (consider, for example, the SGS phones, which do not even have leds).

    That said, id the int field ledARGB was not useful, you might need to look into some JNI call from that APK. My guess is that it will have different methods depending on the device in which is running.

    0 讨论(0)
  • 2020-12-04 16:27

    You can use this code:

    private static final int LED_NOTIFICATION_ID= 0; //arbitrary constant
    
    private void RedFlashLight() {
        NotificationManager nm = (NotificationManager) getSystemService( NOTIFICATION_SERVICE);
        Notification notif = new Notification();
        notif.ledARGB = 0xFFff0000;
        notif.flags = Notification.FLAG_SHOW_LIGHTS;
        notif.ledOnMS = 100; 
        notif.ledOffMS = 100; 
        nm.notify(LED_NOTIFICATION_ID, notif);
    }
    

    Instead of using ARGB value as the example show, you can use int property inside android.graphics.Color class to get the color as well (e.g. Color.WHITE)

    0 讨论(0)
  • 2020-12-04 16:29

    FLAG_SHOW_LIGHTS and Notification.Builder.setLights(int,int,int); are deprecated since Android O ( API level 26 ) If you plan to use this feature in API level 26 or greater have look at NotificationChannel

    Example :

    NotificationChannel mChannel = new NotificationChannel(id, name, importance);
    .....
    .....
    mChannel.enableLights(true);
    // Sets the notification light color for notifications posted to this
    // channel, if the device supports this feature.
    mChannel.setLightColor(Color.RED);
    

    But in this new implementation You may not have control over LED on milli-seconds & LED off milli-seconds it will be dependent on hardware.

    0 讨论(0)
  • 2020-12-04 16:32

    Have a look on source below.

    NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(ctx)
                            .setPriority(Notification.PRIORITY_HIGH)
                            .setSmallIcon(getNotificationIcon())
                            .setColor(0xff493C7C)
                            .setContentTitle(ctx.getString(R.string.app_name))
                            .setContentText(msg)
                            .setDefaults(Notification.DEFAULT_SOUND)
                            .setLights(0xff493C7C, 1000, 1000)
                            .setStyle(new NotificationCompat.BigTextStyle().bigText(styledMsg));
    
    0 讨论(0)
  • 2020-12-04 16:32

    I have tried my code below, and the light works fine for me. My mobile is Nexus 6P:

    mBuilder.setContentTitle("APP_NAME")
                    .setContentText(msg)
                    .setContentIntent(PendingIntent.getActivity(mCtxt, UUID.randomUUID().hashCode(), new Intent(mCtxt, ReceivePushActivity.class), Notification.FLAG_AUTO_CANCEL))
                    .setWhen(System.currentTimeMillis())
                    .setPriority(Notification.PRIORITY_DEFAULT)
                    .setAutoCancel(true)
                    //.setDefaults(Notification.DEFAULT_ALL)
                    .setVibrate(new long[] {0, 1000, 200,1000 })
                    .setLights(Color.MAGENTA, 500, 500)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setSmallIcon(R.mipmap.notify_logo);
    
            Notification ntf = mBuilder.build();
    //        ntf.flags = Notification.DEFAULT_ALL;
    //        ntf.flags = Notification.FLAG_ONLY_ALERT_ONCE;
    //        ntf.flags = Notification.FLAG_AUTO_CANCEL;
    
            mNotificationManager.notify(notifyId, ntf);
    

    Meaning, remove 'DEFAULT_ALL' settings.

    0 讨论(0)
提交回复
热议问题