So I am writing an app that will use the LED to notify users of anything when the screen is OFF. Below is how I used it to work:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.ledARGB = 0xFFFFFF00;
notification.flags = Notification.FLAG_SHOW_LIGHTS;
notification.ledOnMS = 100;
notification.ledOffMS = 100;
notificationManager.notify(Constants.LED_NOTIFICATION_ID, notification);
It works fine, but problem occurs when my device (Galaxy S5) has missed calls, and the stock LED notification for missed calls overrides my LED setting all the time. Is there a way to override the stock LED settings, and have it display mine instead? I've tried fiddling with
notification.priority
but that doesn't seem to do anything. Any help would be appreciated. Thanks!
Is there a way to override the stock LED settings, and have it display mine instead?
Not in general. Control over the LED is up to the device manufacturer, not you. Or, quoting the documentation, "Since hardware varies, you are not guaranteed that any of the values you pass are honored exactly."
Also, bear in mind that not all devices have LEDs that a Notification can control, sometimes because the device does not have an LED at all.
Related
How can I send a notification with sound and vibration when phone is in do not disturb mode on Android Devices.
I use the following code, and it is working when my application is currently in foreground.
PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
resultIntent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText("You've received new message."))
.setContentText("You've received new message.");
// FOR SILENT MODE
AudioManager am = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
// For Normal mode
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
mBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
// Set Vibrate, Sound and Light
int defaults = 0;
defaults = defaults | Notification.DEFAULT_LIGHTS;
// defaults = defaults | Notification.DEFAULT_VIBRATE;
// defaults = defaults | Notification.DEFAULT_SOUND;
mBuilder.setDefaults(defaults);
mBuilder.setSound(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.siren));
// Cancel the notification after its selection
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
I also want notifications with sound and vibration when my app is in background.
Generally speaking you can't. Even notification with MAX priority wont be shown in DND mode.
The possible workaround that I can imagine is to use android.permission.SYSTEM_ALERT_WINDOW to draw custom notification over system window (FB Messanger works in similar way) : Creating a system overlay window (always on top) .
But this method is suitable only in rare cases and most of the time it's a violation of Android UI/UX best practices.
Ok good news you are half way there.
I achieved this previously by using AudioManager & VIBRATOR_SERVICE (yea I know ;)
The idea is not to use NotificationCompat.Builder because it will rely on system settings and if device is in silent mode, it won't vibrate nor play sound. You have to manually play a sound using AudioManager & vibrate using VIBRATOR_SERVICE.
You are using AudioManager & setting the properties but you are never actually asking it to play the sound. So, in your code its never actually utilized.
Here's an example of how to play a sound using AudioManager, it will also ignore silent mode:
Here's an example of using VIBRATOR_SERVICE:
Combine these 2 approaches and ditch NotificationCompat.Builder
If you can get right permissions to change settings of device, then you could show notifications with sound and vibration at your apps. Also check this out.
I've implemented red LED light for my notifications for test purposes, but...
Do I have to check if the device has LED (in case that device doesn't have this feature) and if so how to do it? Also, are LED colors predetermined?
I tried to find some docs or thread that would give me an answer for these questions but had no luck...
There is no way of doing this.
According to Can I detect the presence/absence of 'LED notification' on an Android device?
Most hardware features are exposed via the package manager:
PackageManager pm = getActivity ().getPackageManager ();
boolean hasBluetooth = pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
But not this one.
There is a feature request on record as well:
https://code.google.com/p/android/issues/detail?id=38481
You don't have to check the availability of the LED. the code won't crash even if there is no LED. you can set any color as a hexadecimal color code.
Notification notification = new Notification();
notification .ledARGB = 0xFFff0000;
notification .flags = Notification.FLAG_SHOW_LIGHTS;
The following code works great on a Motorola Defy with Android 2.3.3
However it's not working on a Nexus 4. The LED itself should be fine, the app color led tester from the market works.
NotificationManager notif = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
final Notification notification = new Notification();
notification.ledARGB = Color.RED;
notification.ledOnMS = 1000;
notification.ledOffMS = 300;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notif.notify(1, notification);
Does anybody know what will do the trick here?
I just have tested your code on my Nexus 4 and it's work. But before the test I have locked my phone. Probably this LED will be highlighted only when screen is off.
I'm using a notification to let the user now that the service is still running. Now I'd like to use the notificationlight to remind the user. (because it's fancy)
The notification works fine, but the notification light does nothing. Other applications work fine with the notification light, (gtalk, facebook)
it's more or less the example code for notifications with addition of these flags:
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 100;
notification.ledOffMS = 100;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.flags |= Notification.FLAG_NO_CLEAR + Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(NOTIFICATION_ID, notification);
and
notification.defaults |= Notification.DEFAULT_LIGHTS;
instead doesn't work either.
I'm debugging on a Galaxy Nexus with Android 4.0, but the app's target is Android 2.3.3
EDIT:
could this be a problem of permission? If yes, which one? I looked through all and found no matching permission for the notification light.
I think there is an error with the + operator, you need the OR:
notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
EDIT: and if you are using flags, I think the right one should be:
notification.flags |= Notification.FLAG_SHOW_LIGHTS
On jelly bean devices, led only works if notification priority is set to max or default, please check again. Following snippet of code is working fine for me on jb devices.
notification.setLights(0xFF0000FF,100,3000);
notification.setPriority(Notification.PRIORITY_DEFAULT);
Here I'm showing blue color led for notification which will remain on for 100 ms and off for 3000 ms till user unlocks his device.
And check if you are using NotificationCompat (compatibility) class than ignore setDefaults method and use SetLight, SetSound, Setvibration, etc
My old Nokia phone was flashing hardware buttons when I missed a call. So I was able to understand that I have missed a call just by looking to phone. With my new Android phone I have to reach my phone and wake the screen to see if I have missed a call.
I have searched Android market but could not find exact application to solve my problem. So I have decided to write it. The question is how can I turn on and off back lid of hardware buttons of a android phone?
I have googled it but could not find a clean answer.
Thanks in advance.
Android does have notifications for that purpose, the backlight is not thought to be controlled through the API (you could do it on rooted devices but thats another story).
Personally, I dedinitely do get notifications for missed calls, and my notification LED blinks. However, you can implement your own notifications:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// create a new notification
CharSequence tickerText = "Missed call";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
// control how the notification led should behave
notification.ledARGB = 0xff00ff00;
// blink for 300ms every 1s
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
// usually you also want to create a PendingIntent and attach it
// with notification.setLatestEventInfo
// finally, post the notification to the notification manager
notificationManager.notify(HELLO_ID, notification);
There are many other options for notifications, like vibration or FLAG_AUTO_CANCEL, but they are documented very well ;-)
On a rooted device, you could execute the following to control the backlight (however, I would recommend sticking to the intended way, which are notifications):
su
echo 25 > /sys/class/leds/button-backlight-portrait/currents
echo 25 > /sys/class/leds/button-backlight-landscape/currents
where 25 would be the brightness. But admittetly, I don't know for sure if this would even work on all devices.