Dialog box from Service - android

I am running a service as soon as application starts. That service will keep checking SQLite for data. If it finds a certain data, a dialog box should pop up (user can be on any activity). Don't want to use notification. How do I pop up Dialog box from a service ?

Short question is you cant. However, you can send an intent to your activity for it to be the one showing the dialog box, or you can start an activity that looks like a dialog.

Related

Displaying a AlertDialog on screen irrespective of Activity Screen

I want to show an Dialog Whatever the screen the User is in. Suppose if user opens application and in initial screen if I receive a server message I have to show it in a dialog.Meanwhile there is an option of autologin . So it could move to my next activity.If this is the case that dialog should not be closed.It should show on newly opened activity rather than the previous activity.And other thing is that even though the dialog is shown I should be able to control my buttons on the activity.
Here's what i am doing.
if(Activity1.mcontext!=null){
CommonMethods.showDialog(sliderMessageText,
LoginActivity.mcontext,"activity1");
}
if(Activity2.context!=null){
CommonMethods.showSliderMessageText(sliderMessageText,
Activity2.context,"activity2");
}
if(Activity3.mcontext!=null){
CommonMethods.showSliderMessageText(sliderMessageText,Activity3.context,"activity3");
}
Instead I am displaying the dialog in all the activities.
Thanks in advance.
You may want to consider using an Event driven model such as GreenRobot.
http://greenrobot.org/eventbus/
This would allow you to efficiently handle the scenario you describe.
Alternatively, you can use the LocalBroadcastManager to communicate between different parts of your app.
e.g Send a broadcast message when you want to display a dialog and handle that message in all of your Activities

Is it possible in android to leave an AlertDialog in front of changing Activities?

I have an app with multiple Activity classes. In one part, one is a main screen and the next is a login. The user would open the login screen and enter their info and press a button to submit it. The resulting code opens up an AlertDialog to tell them the registration status, along with some other information. If the registration itself was successful, they will return to the previous activity. But this makes any AlertDialog disappear with it.
I want the background to return to the previous activity so they can see the home page start updating, but I want them to see the alert message long enough to read it.
Yes this is possble. By using a service you can display an alertbox which runs in foreground for a long time. I have done thissame kind of thing for a news app.
It will run in foreground for a long time and when you want to switch you can stop the service than it will dismiss the alert box-.
In my case I displayed a custom view with buttons in the foreground.

Maximize android app from service

I have an application which runs background service which checks notifications which it gets from some server. And when the notification arrives (Ping notification), it opens a new Notification dialog with full screen inflated view. On this screen you have an option to accept the call, which calls another device with SIP voice connection. This accept call opens up a new dialog fragment, which has the ability to automatically answer the call, and the user has the option to cancel it. When I am on main activity window, this works just fine. In service there is view inflated with layout added to window_manager which shows notification and when you accept the call, dialog fragment with call status opens. The question I have now, is it possible to do that while application is minimized?
So for example, user minimized app, service is still running in the background, when the ping notification gets to the device, it shows new notification dialog, but when you want to accept the call and to show fragment, it is not possible because.
1. I don't have current screen on which to show dialog fragment with calling
2. If I create some instance of the current Activity onCreate, and I don't destroy it onDestroy, I can call startActivity on it, but nothing shows up. Call establishes, but I cannot maximize my current activity and show dialog fragment on it.
What can I do in this case? Should I also create some other notification like this one in service and instead of dialog fragment I should be showing another notification?

android alertdialog with broadcast receiver

I have a network monitor (using Broadcast receiver) in my app, what it does it pops an alertdialog if the phone is not connected to internet and if the phone connects back it pops another alert informing the user he is back online. The problem is every time the monitor sees a state change it creates another window that stacks on top of the other one. For example if the phone has its DATA ON/OFF 3 times it will pop 6 messages. Is there a way of dismissing an alert dialog if another one opens or what would be the best approach to overcome this scenario?
Thank you in advance, your help is very much appreciated.
Android newbie
Add a variable containing the currently visible dialog:
Alertdialog a;
and then when you want to show one:
if (a!=null) {
a.cancel();
}
a = yourDialogBuilder.show();
Hope this helps.
Dialog has an isShowing() method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog().
I think you should consider re-designing your notification mechanism. Using Dialog can be very disturbing to the user experience since it requires an action to dismiss and interrupt the natural flow of the app.
Consider using Toast for simple notifications or Notification if you want it to show in the statusbar. Also, you can have some icon in your application that reflects the current connectivity status e.g. green is connected and red otherwise.
To ensure only one dialog is available, you can create a static instance of your dialog in your activity and dismiss it if it is not null before showing a new dialog

The AlertDialog is invisible when the Activity back to foreground

I write a program support download file. When in this download activity, I start a progress bar and then run a thread to do the download things. This thread send message to UI thread to notify how many has been downloaded frequently. In the main(UI) thread, I update the progress bar display when receive the message. If any problem happen in download progress, it will send another message. When main thread receive the message, it stop the progress bar and pop up a new AlertDialog to show the error reason.
Here is a special test for it. When downloading, switch the program to settings. Turn off the WIFI/GPRS to make the network off.
When I back to my program, it should display the background ui and a pop up AlertDialog to show the reason as I wish. But it only display the background ui(which means the main activity) and with a half-light of backlight just as the popup windows still there. When I press back for first time, nothing happens except the backlight is bright just like I have close a pop up window.
I think maybe it is because when I start the AlertDialog my activity is not in foreground.
I tried to use:
ActivityManager am = (ActivityManager)Update.this.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
to judge whether the top activity is mine. But I want to show the dialog. If the activity isn't being seen by the user, when he gets back to my activity, I want it to show the dialog.
Indeed the dialog already shown but not visible to the user and it would become visible if you rotate the device.
There seemed a refresh/drawing issue if dismiss a progress dialog and show another new dialog immediately when the activity is not in foreground.
During my testing, such issue not happen if wait for the dismiss action finished for the progress dialog and then show the new dialog.
So one solution is that show the AlertDialog first and then dismiss the ProgressDialog. That worked for my application.
I think, there is some king of bug in Dialog. I have the same situation, and only solution was dismiss curent dialog by dialogDismiss(id) and show it again by showDialog(id).

Categories

Resources