I'm trying to turn the LED-flashing on for my activity in foreground, but it works only when the screen is off.
Is it possible to turn the LED on for active activity with the screen on?
My code:
protected void led() {
Notification notif = new Notification();
notif.ledARGB = 0xFF0000ff;
notif.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_ONGOING_EVENT;
notif.ledOnMS = 800;
notif.ledOffMS = 200;
notificationManager.notify( LED_NOTIFICATION_ID, notif );
}
At first Android LED indicator is very hardware dependent. Second - there is no API for managing LED instead of Notification class with its FLAG_SHOW_LIGHTS flag and several flags for managing flash duration and LED color which you use. Notification is a message you can display to the user outside of your application's normal UI the primary purpose of LED indicator is to present additional notification information for the user when the screen is off. So the answer is definite NO. The LED will only start flashing if your screen is off and it’ll stop when you turn it back on. And there is no way to turn on and off LED when you want and to turn it when any of the application activities is in foreground, because it is managed by the OS internally.
Related
I m trying to put my notification in the Top Area of the Notification Bar.
(Like a High Priority Non-Dismissible Alert)
Few apps are already doing it, like Google Meet - On Going call red notification always remains on top.
Google Meet Showing Notification always on top
Similarly Phoenix Browser Does with its News Bar Notification.
Phoenix Browser showing in similar manner
Both Apps Showing notification together on TOP
Both Google meet and Phoenix Browser Together always on top
NOTE: These are not periodic notifications that are updated to be kept always on top.
Notification count in settings is always 2 times a day.
These Notification is above Conversation Notification as well, and always remain on top.
Combinations tried to achieve till now:
By Setting notification Flags to OnGoing / non-dismissible
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
By Changing Time of Notification
notification.when = timeNotification; //setting timeNotification as one minute ago and a min later as well.
By Setting Priority to Max / High
notification.priority = Notification.PRIORITY_MAX;
By changing Category of notification
notification.setCategory = Notification.CATEGORY_EMAIL //To Transport/ Call/ Alarm / etc
Let me know, if I'm missing something to achieve this
I'm changing an existing application to use MediaStyle notifications to provide lock screen music information and transport controls in Android 5.0. It seems, however, that updating such notifications isn't working as expected. The following code snippet generates notifications each time a button is pressed, incrementing a counter displayed in the notification title:
public class MainActivity extends Activity {
private int serial;
private TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView)findViewById(R.id.textView1);
}
public void buttonClick(final View view) {
final Notification.Builder builder = new Notification.Builder(this)
.setContentTitle("Title " + serial)
.setContentText("Text")
.setContentInfo("Info")
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher))
.setOngoing(true)
.setStyle(new Notification.MediaStyle())
.setVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE );
nm.notify(1, builder.build());
text.setText("Serial = " + serial);
serial++;
}
}
The problems are:
When executed in the emulator running 5.0, the notification text in the notification drawer or lock screen is out of sync with the serial displayed in the TextView. If MediaStyle is not set, notifications have correct numbering in the notification drawer, but not in the lock screen.
The MediaStyle notification is displayed correctly in the lock screen, until it's viewed in the notification drawer for the first time. After that, it's not displayed as a MediaStyle notification in the lock screen anymore (but not exactly like a standard, non-MediaStyle notification). Notably, buttons added with addAction() are not displayed anymore, until the emulator is restarted.
So I suspect I'm doing something very wrong (either that, or the Android image running on the emulator is broken, but that seems less likely). Any ideas?
It turned out that the emulator was broken after all. The notification behavior, including updating icons added with addAction(), title and info updates, and notifications in lock screen after being seen in the drawer, is correct in a real device (a Nexus 5 running 5.0). At the time this answer was written, however, the problems still happened in the emulator provided with the API 21 SDK, and no updates were available.
I have an application that has a reminder feature. When it's time to remind the user of something, my application creates a notification, possibly using FLAG_INSISTENT to ensure the alarm is heard. Once the user interacts with my app to acknowledge the alarm, the app cancels the notification.
The user can launch the app either by pulling down the notification bar and tapping on my notification -- in which case everything is fine -- or by navigating to the app some other way, such as by launching it from the home screen. If the user uses the notification bar method, the FLAG_INSISTENT audio stops when the user touches the notification bar. But here's the problem: if the user enters the app directly without touching the notification bar. the audio for the FLAG_INSISTENT alarm keeps playing indefinitely -- even after my app cancels the notification. The only way a user can stop it is to pull down the notification bar (or reboot the device!).
I've been getting tons of bug reports from angry users ever since the optional FLAG_INSISTENT feature went live. It doesn't seem specific to one platform; users reporting this bug have hardware including a Motorol Razr Maxx HD, Samsung Galaxy Note, and HTC EVO 4G LTE. I've had frustrated users report that they resorted to uninstalling the app to stop the noise, and even then said it wouldn't stop. Searching the web has been fruitless.
The notifications are being created in more-or-less the garden variety way:
notification = new Notification(
R.drawable.icon,
message,
System.currentTimeMillis()
);
if (userDefinedaCustomSound) {
notification.sound = Uri.parse(userSelectedReminderSound);
} else {
notification.defaults |= DEFAULT_SOUND;
}
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.DEFAULT_VIBRATE;
if (userWantsContinuousAlarm) {
notification.flags |= Notification.FLAG_INSISTENT;
}
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_BAR_ID, notification);
And are being cancelled thusly:
nm.cancel(NOTIFICATION_BAR_ID);
I've tried adding the FLAG_AUTO_CANCEL to the notification; that has no effect. As a workaround, I've also tried modifying my cancel method so that it first issues a new notification with no sound, and without FLAG_INSISTENT, then cancels; again, the audio just keeps on playing.
Any ideas?
I faced the same problem. The audio keep on playing after the notification was cancelled.
I formulated this work around,
Perform a normal notification cancelled.mNotificationManager.cancel(getNotificationID(event));
Immediately Create another notification with sound. Use the default Alarm.
Immediately cancel the notification.
Note:
The sound played as part of the notification was not the default.
method getNotificationID(event) always return the same constant for the same event object type.
Notification with sound played using the default will stop when the notification is cancelled.
I set the sound using builder using this
setSound(Uri.parse(this.sharedPreferences.getString(key,"")))
From the observations, I think it might be a bug. The reference to the ringtone object was not properly retained so when the cancel was called, it failed to call on the ringtone .stop() or was unable to do so.
Hope you can used it too.
NotificationManager mNotificationManager = (NotificationManager) this.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(getNotificationID(event));
audioAlarmTriggered.remove(event.sensortype);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setAutoCancel(true)
.setSound(ringtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
mNotificationManager.notify(getNotificationID(event), builder.build());
mNotificationManager.cancel(getNotificationID(event));
If above code is in your service class. Then I think the problem is when you open app directly instead using notification bar, that time its not calling your service class else you need to call nm.cancel(NOTIFICATION_BAR_ID); from the activity which opens while clicking on notification.
And to do so you need a global class which keeps static NOTIFICATION_BAR_ID, so that will be helpful to you for managing cancel method.
I hope, this will solve your problem.
I cast an LED notification this straight forward way:
NotificationManager notifMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notifMgr.cancelAll();
Notification notif = new Notification();
notif.ledARGB = 0xff0000ff;
notif.ledOnMS = 99999;
notif.ledOffMS = 0;
notif.flags |= Notification.FLAG_SHOW_LIGHTS;
notifMgr.notify(1234, notif);
It let the LED just permanent light blue.
If, in that state, I receive for example a google talk message, it's notification overwrites my LED state, so the LED now blinks white (gtalk default).
If now my program creates another LED notification, for some reason it does not overwrite the gtalk LED notification, so the LED stays blinking white.
How can I make my notification overwrite other LED notivications? Obviously there must be a way, since gtalk overwrites my LED state.
Thank you!
Whichever app raises the notification first keeps control of the led until it's cleared by that app or the user clearing it.
You description sounds incorrect of gtalk overriding your notification if yours is currently on. I know a little about this as I'm the author of "lightflow"
I have a count down timer that when it goes off (to zero) it checks to see if the app has focus. If not it launches a notification in the notification bar. When you click on the notification is re-opens the app. Now all of this works fine but if the screen happens to go off, the timer keeps going and the notification is available at the right time but never actually vibrates or rings until i turn the screen back on. Then it displays the notification like it was waiting in a queue or something.
How do I get it so that the notification manager will actually alert the user when the screen is turned off?
Update: If I set the timer for 2 minutes, it takes another 2-3 minutes for the notification to actually work. So it does work but it's on a huge delay!
Code: So I setup the notification service when the app loses focus, and when the MyCount1 is finished is checks if the app has focus and if not it shows the notification. This all works when the screen backlight is on. Once it goes off it is unreliable.
#Override
public void onWindowFocusChanged(boolean hasFocus){
if(hasFocus == false){
mFocusFlag = false;
ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(ns);
icon = R.drawable.statusbar;
tickerText = "Check the timer!!!";
when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
context = getApplicationContext();
contentTitle = "Countdown Timer";
contentText = "Click to Check the Timer";
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationIntent = new Intent(this, StartTimer.class);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
}else{
mFocusFlag = true;
}
}
public class MyCount1 extends CountDownTimer {
public MyCount1(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
if(mFocusFlag == false){
mNotificationManager.notify(HELLO_ID, notification);
}else{
mVib.vibrate(1000);
}
}
public void onTick(long millisUntilFinished) {
if((millisUntilFinished/1000%60) < 10){
mTime.setText("1st Side = " + millisUntilFinished/60000 + ":0" + (millisUntilFinished / 1000)%60);
}else{
mTime.setText("1st Side = " + millisUntilFinished/60000 + ":" + (millisUntilFinished / 1000)%60);
}
}
}
Now all of this works fine but if the screen happens to go off, the timer keeps going and the notification is available at the right time but never actually vibrates or rings until i turn the screen back on. Then it displays the notification like it was waiting in a queue or something.
How do I get it so that the notification manager will actually alert the user when the screen is turned off?
When the screen turns off, the CPU will stop running shortly thereafter, unless something is holding a WakeLock.
This means one of two things:
You understand all of this and are holding a WakeLock. This may or may not be a good idea, from the standpoint of what users like with their devices (e.g., good battery life). Be that as it may, you may need to hold a stronger WakeLock, one that keeps the screen at least dim. I have not tried raising a Notification while under a WakeLock, so I am uncertain what the rules all are.
You do not understand all of this, and therefore are thinking your timer is going when, in reality, the device has fallen asleep and the CPU has turned off. When the CPU turns back on again, your timer will go off immediately.
Using AlarmManager allows you to do timer-based events that wake up the device, and do not require your code to be hanging around in memory in the meantime. I have no idea what you're trying to do here (seems rather bizarre from your description), but AlarmManager may be something worth investigating as a replacement for your timer.
It probably goes off when the phone is woken up by some other application, for example email application periodically goes to check for new emails. You need to set your own alarm to fire at the time you need. http://developer.android.com/reference/android/app/AlarmManager.html
NotificationManager doesn't have notion of time. It has to be triggered somehow. The problem in your case is that, if you let the phone sleep, it will do just that. Alarms are specifically exist so you can wake up the device when you need to.