In my app, I have to continuously show status bar notification after some event is triggered. The status bar must glow in red rather than in default background. During that period,I am responsible for recording sound i.e. it is just like recorder but the instead of running in Activity, it will run on Service and the user is notified by blinking status bar in red. The user at any time can go the status bar and stop the recording.And the recording can carry on even when lockscreen is on.
So is it possible to achieve this in Android and if possible could you give us some logic for this implementation.
Consider using a foreground service, which lets you post an ongoing notification. There's no API for changing the color of the entire status bar. You can certainly color your icons, and on JellyBean and later you may be able to change the color of the notification itself using RemoteViews -- that's talked about in this link.
Related
I have used foreground service with a custom notification layout. Everytime the notification is present the lock screen and the status bar gets very slow. Everywhere the notification is shown that part of the mobile UI becomes slow. Whenever I open such places phone goes 10Hz display. This happens only in few devices.
Currently, I have a mobile application written in Xamarin.Android. Because there are constant HTTP requests going on in the app, what I'd like to do is keep the application active after closing it.
What I am looking at, is for example after you close the app, in the background it keeps sending HTTP requests, but there is a local notification where when you click it you can open the app. There should be an explicit button for the exit which could be inside the application. In more details, HTTP requests are sent on some interval in seconds.
So my end goal is to have the app constantly running, if it's in a background, then show a notification that it's still up and be able to close it (even from the background) only from the inside of the app.
I couldn't find many resources specifically about this question and I am not quite sure what I can use in order to make this work.
Thanks in advance!
According to the documentation what you're trying to do is supported:
Sending notifications to the user
When a service is running, it can notify the user of events using
Toast Notifications or Status Bar Notifications.
A toast notification is a message that appears on the surface of the
current window for only a moment before disappearing. A status bar
notification provides an icon in the status bar with a message, which
the user can select in order to take an action (such as start an
activity).
Usually, a status bar notification is the best technique to use when
background work such as a file download has completed, and the user
can now act on it. When the user selects the notification from the
expanded view, the notification can start an activity (such as to
display the downloaded file).
See the Toast Notifications or Status Bar Notifications developer
guides for more information.
ref: https://developer.android.com/guide/components/services
ref: https://developer.android.com/guide/components/processes-and-threads
Background
Android Lollipop (API 21) introduced a way to show notifications outside of the status bar (AKA "notifications bar"), so that the user can handle them right away. It's called "Heads-up notifications".
The trigger for showing them may vary between devices/roms/manufacturers.
The problem
Sometimes, showing such notifications can annoy users, and most of the times there are no settings for those cases.
If the user dismisses heads-up notifications, they won't show as a normal notification. There is no way to hide them and continue with what's on the screen. You can only wait (and it's quite a long time of waiting too).
In fact, there are multiple Google-Group issues that were opened about it, just because it can annoy people (link here and here).
What I've found
Starting with API 18, it is possible to listen to notifications events and even read them, by using "NotificationListenerService" and "StatusBarNotification" , and maybe other classes.
However, other than dismissing notifications (of other apps), I can't find any other action that can be done to them.
The question
Is it possible that in the lifetime of my app, I will be able to listen to notifications that are shown as heads-up, and put them back as a status-bar notifications?
Maybe even set a different timeout for them? or choose to convert them to normal status-bar notifications when they get dismissed?
Maybe before even doing those operations, I should ask: how can I know if a notification that I've found (of other apps) is showing as a heads-up notification ?
I don't know how to do implement this. But answering the "Is it possible that...?" question, yes, there are apps like this one that block/only show notifications in the notification bar.
First, do this (tested on Nexus 5 running 4.4.2):
Pass a PRIORITY_LOW notification to Service.startForeground().
Observe notification is not shown in status bar.
Raise a PRIORITY_MAX notification using same notification ID.
Observe notification is shown in status bar.
Now, is there any way to remove that icon from the status bar (other than Service.stopForeground())?
I've tried calling NotificationManager.cancel() and NotificationManager.cancelAll(). And also tried raising the same original PRIORITY_LOW notification. But all of them leave the notification icon in the status bar.
This was changed in Android 4.3, to always show notifications for foreground services.
There is a good reason for this, namely that foreground services cannot be killed when the system needs more resources. If the notification is not there, there is no way for a normal user to know that there is something still running and draining battery.
If you don't want the notification, consider using a background service instead. If you really need a foreground service, the user have the right to know.
Read more on http://commonsware.com/blog/2013/07/30/notifications-foreground-services-android-4p3.html
One workaround is to call Service.stopForeground() followed by Service.startForeground().
When a notification arrives it hides the network signal and battery status icons.
Is there any possibility to show the notification without hiding the network icons?
When a notification arrives it hides the network signal and battery status icons.
For about 1-2 seconds, while the ticker text is being displayed.
Is there any possibility to show the notification without hiding the network icons?
AFAIK, no. You could try an empty/null ticker text and see what happens -- I haven't tried that.