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.
Related
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 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.
I have a very hard time to finding real solution.I searched a lot but I cant find any good solution for my problem.
I want to perform some action when user click on notification.So I have to check whether application in background or foreground. If it is in foreground then which activity is running ? There must be simple way to find this.
Please Guide me on this.Any help will be appreciated.
You should listen in two places of your application:
In your activity to show the alert.
In your service to show the notification check if activity is running using one of following solutions.
You have severals answers there summarizing the solutions:
Android: how do I check if activity is running?
Checking if an Android application is running in the background
Because If application is in background then show notification otherwise show alert.
Then you do not care "whether application in background or foreground" or "If it is in foreground then which activity is running".
You simply care that, when the event occurs, either the foreground UI handles it (if there is a foreground UI), or else you show a Notification.
One approach is to use an ordered broadcast, as I blogged about a couple of years ago. Here is a sample app that demonstrates this. Basically, the service(?) sends an ordered broadcast. The UI has a high-priority receiver for that broadcast when it is in the foreground, and it aborts the broadcast and consume the event. You also have a low-priority receiver, one that will only get control if the UI did not consume the event, that will display the Notification.
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.
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.