Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing LED color for notifications

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 notifications in different colors (blue, green, orange, pink and light blue). So, I have tried to do this myself in my own app, however I cannot change neiher the color nor the blinking internal of the LED. I currently use this code:

public class MainActivity extends Activity {   static final int NOTIFICATION_ID = 1;    @Override   public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);      Button button = (Button) findViewById(R.id.button);     button.setOnClickListener(buttonOnClick);   }    public OnClickListener buttonOnClick = new OnClickListener() {      @Override     public void onClick(View v) {       String ns = Context.NOTIFICATION_SERVICE;       NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);        Notification notification = new Notification(R.drawable.icon, "Hello", System.currentTimeMillis());       notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL;       notification.ledARGB = Color.BLUE;       notification.ledOnMS = 1000;       notification.ledOffMS = 300;        Context context = getApplicationContext();       CharSequence contentTitle = "My notification";       CharSequence contentText = "Hello World!";       Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);       PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);        mNotificationManager.notify(NOTIFICATION_ID, notification);     }   }; } 

But as I said, it doesn't work the way I want it to; instead it just blinks in regular green with the default delay, and not the one I have set in my code.

Can anyone see what is wrong with my code, or know if I have to do something else to achieve this?

like image 396
Frxstrem Avatar asked May 29 '11 17:05

Frxstrem


People also ask

How do I change LED notification color?

To change the colour, open the app, then go to the app's settings menu to find out which options are available. You can turn LED notifications on or off in the “Settings” menu.

How do I change the LED notification on my Android?

If the LED indicator behavior does not work as intended, check the LED notification settings. Follow these steps to turn on LED notification: Navigate to Settings > Display & gestures > Notification light. Tap the setting to turn it On.


2 Answers

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)

like image 141
Stuti Avatar answered Oct 05 '22 13:10

Stuti


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.

like image 43
Aleadam Avatar answered Oct 05 '22 13:10

Aleadam