i want to pop-up a dialog while some specific event occurs. Say i am an antivirus and found a virus, i can't send notification for this right, user could be compromised even dead until he checks the notification.
So i want to show a dialog like allow-deny, or yes-no, not to disturb user, it's gonna happen very rarely.
So is there any way that a service or application shows a dialog while user is using another application, even playing a game.
Thanks in advance...
Have a look at the Notification documentation. I don't believe you can/should create an AlertDialog when its not in the foreground. See here for Android's information on design patterns regarding when you should and shouldn't use them (you're interested in the last paragraph.
To quote the documentation.
Dialogs and toasts are for feedback not notification
Your app should not create a dialog or toast if it is not currently on screen. Dialogs and Toasts should only be displayed as the immediate response to the user taking an action inside of your app. For further guidance on the use of dialogs and toasts, refer to Confirming & Acknowledging.
This is a better, less intrusive way of notifying the user in your specific case. I also know that its possible to create a notification from a background service.
You create the Notification using the Notification.Builder class, setting things like title, icon, text, when to display it and also the intent to launch when the user clicks on the notification (used to open your app at a desired location). Once created you then use the startForeground(NOTIFICATION_ID,notification). You specify an id so you can access the notification later.
Related
In my app I'm publishing notifications for chats, and whenever a chat is read by user, I want to remove the notificaton. If the user has opted to open a bubble, however, I want to preserve it until the user manuall dismisses it.
I can easily do this when the user opens the relevant chat in the bubble itself, or in the main activity, by republishing the notification with .setSuppressNotification(true) called on its bubble metadata. The effect is what I want exactly; the notification is gone, the bubble stays, and the badge is cleared on the bubble.
The problem is, the user can also read the chat elsewhere, not on the device. In this case .setSuppressNotification() might not work:
This flag has no effect if the app posting the bubble is not in the foreground. The app is considered foreground if it is visible and on the screen, note that a foreground service does not qualify.
Is there a way to remove a notification without removing its bubble while the app is in background?
P.S. What do the other apps do?
Telegram seems to be preserving the bubble, only removing relevant notifications when you open the bubble itself. The notifications will stay even if you read the chat in main activity and dismiss the bubble. I don't think this is acceptable;
Signal, on the other hand, will kill the bubble whenever you open the relevant chat in the app. This is perhaps reasonable for one-to-one chats, as the bubble will reappear on a new message anyway, but for group chats I would like to preserve the bubble;
People (the sample app) will preserve the bubble while removing the notification. Being not a real messenger, it doesn't have to deal with my specific use case, however.
You can try NotificationListenerService
Request user to enable permission Notification access
Cancel Notification using NotificationListenerService
do not use NotificationService#cancel
I found a Github sample here (not mine): https://github.com/Chagall/notification-listener-service-example
You can follow the sample here and implement some more code to make it happen
I created an issue about this on Google's tracker and they marked it as fixed, saying
We would like to inform you, setSuppressNotification will work regardless of the app being in the foreground and this change has been introduced in Android S.
I've been trying to look up solution but I just can't seem to find one that suits my need. It could be that I'm not using the correct keyword, so I'm gonna try to explain it the best I can.
Simply saying, I'm building an Android application. The application need to detect some other activities on the phone and show some message.
For example:
User launch MyApplication.
User received some notification from another app. (a phone call, text message, email, whatever)
MyApplication shows a message via toast or dialog, saying that "you just received an notification from another app"
I'm not entirely sure what function I should be looking for here, any help is appreciated.
Thanks!
You will want to look at NotificationListenerService, this will let you receive calls from the system when new notifications are posted or removed, or their ranking changed.
Once you receive a callback from the NotificationListenerService, you will want to create a Toast.
Take a look at this tutorial should help you get started.
To detect when other apps ask the OS to display a Notification, you would need to implement a NotificationListenerService. The user would then not only need to install your app, but also go into Settings and specifically allow you to monitor notifications.
I was wondering if there is a way to programatically set the phone's indicator icons, like GPS or upload/download in progress,etc.. ?
You have to use a Notification. Set the icon when initializing Notification class. You can get more details here
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Yes, Notifications is what you are looking for. If you are notifying the user about something that just happened (for instance, a message arrived), you should use a plain Notification 1. However, if you want to notify the user that a service is running, then you should call startForeground() from your Service 2, and stopForeground() when you are done with the operation.
Some points to keep an eye out for:
Only use notification for things the user cares about. This may seem pretty obvious, but you'll find a surprising number of applications that notify the user of some background maintenance process that they don't really care (or can do anything) about.
Let the user customize what sort of things should trigger a notification. It might be very annoying for the user if the phone is constantly beeping and vibrating.
Every notification should have an appropriate Intent that allows the user to take action on that particular notification, and that Intent should be the one that makes the most sense for that particular event -- for example, when the user taps a "new message" notification, he expects to be let to a screen where he can read the message. Sending the user to your application's home screen in that case will be much less useful.
At present i am working on c2dm in android.i am sending messages from server but messages are displaying on Log cat.But i want to display dialog when message occur how can i do this.
Please help me.
Maybe you should use the notifications framework to notify the user by dropping an icon in the notification bar. This is less obtrusive, but gives the user some visual cue that something has happened.
First, you cannot display dialog boxes from broadcast receivers. At most, you can start up an activity, perhaps one themed to look like a dialog.
Second, popping up an activity like this is generally frowned upon. You have no idea what the user is doing at the time, and they may not appreciate your activity taking over the foreground. Only a select handful of applications (VOIP clients, alarm clocks, etc.) should do this sort of thing.
I m a newbie to android development and i wanna know a right way to get notification as an activity instead of statusbar notification.
To be clear, i wanna display an notification on screen instead of adding it to status bar.
In that case you shouldn't call it notification. In Android, a notification is something that ends up in the status bar and that the user can deal with at her own leisure.
An activity will take over the complete screen and force the user to deal with it immediately. That is very intrusive and users will likely get annoyed, unless the information you show is really crucial.
The correct behaviour is to post a notification and bring up your activity when the user clicks on the notification, very much like your messaging client or email client.
Other options are bringing up a dialogue or a toaster message. A dialogue is a message the user will have to interact with to go away, whereas a toaster message will be visible for a short time and then automatically disappear.
Maybe you could be a bit more specific about what sort of information you want to provide to the user?
Surely you can be helped out.
I feel that you are getting notifications from a web service as a response to some request, right?
If yes, then you can display them in a custom made dialog box, or inside an activity whose theme has been set as DIALOG.
maybe I got your question wrong, maybe your talking about the notifications about networks, calls and things.
If its so, correct me.
Thanks!
Android 2.3 introduced a new field on Notification, fullScreenIntent, for exactly this purpose. Place a PendingIntent here that will trigger whatever activity you'd like to display, and the user will be sent to that activity (whether full-screen or dialog-themed) in addition to receiving the usual notification icon in the status bar. As of Gingerbread, this is how the Phone app implements the incoming call screen.