Shown an alert from a non activity - android

In my app there is a polling task which calls a web service periodically. According to the response of this webservice I need to display an alert dialog with two buttons.
But I'm not able to display the dialog as an alert dialog can be showned only from an activity. Only toasts are able to display. Is there any methods to display the alerts?

Showing Alerts from a background process is not a desired functionality.
I would suggest you to display a Notification in the Notification bar.
You can start an Activity from your Background Task with
Intent myIntent = new Intent(getApplicationContext(), MyActivityNameGoesHere.class)
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(myIntent)
In this activity you can start a regular dialog.

Related

how to show custom dialog box when android application receives notification

popup message feature like whats app, caller id like the true caller, I am trying to make the same thing.
I tried using a broadcast receiver but I do not know how to show the dialog box when a broadcast receives a notification.
a pop-up message like WhatsApp shows when a notification arrives on a phone.
Set mainactivity as a launcher screen. In the mainactivity before setContentView method calling check if the intent has extra parameters as per your requirement. If yes, then finish the current activity and start DialogActivity.

How show Dialog when app minimized and above another app if this app opened?

I have some Dialog. It shows when my app receipt message and I state in my Activity. I want show this Dialog if app minimized and not show Activity. (It is implemented in WatsApp). If pened somp app on phone, I want show dialog above this app.(It is implemented in WhatsApp too)
It executed when I receipt message and app is opened:
m_orderOfferForm = new NewOrderForm(BaseActivity.Instance, offerOrder);
m_orderOfferForm.show();
You can achieve this by background service continuously running for you and get the event of receiving or what you want to do. Then create intent for NEW ACTIVITY and in that intent create Dialog.!!!
Njoy. :)
This can be achieved using an activity as dialog. Run service in the background and on event trigger call activity from service.
Design of your activity layout should be like this
<Framelayout>
<Linearlayout
margin:20dp
background:translucent image(check for menu.popup.9.png)
layout_gravity:center>
<Content>
</Linearlayout>
</Framelayout>

Android Services, Activities and Handlers?

Im developping an Android Service in Android that needs to pop-up a new dialog box for confirmation.
I can popup a new activity using Intent and Context
Intent myIntent = new Intent(context, ConfirmationActivity.class);
But then I need to handle the option selected in the dialog box (OK or Cancel).
Any suggestion?
Note: Not developing for smartphones.
Update: I need to return the result to the place I call the Dialog.
A service should not pop up anything at all. Imagine the user is in the middle of a phone call when your dialog pops up.
If you need a confirmation from the user the best thing you can do is to use the NotificationManager and use a PendingIntent to launch an Activity. The Activity can still have a dialog style if you like.
Control flows back from the Activity to your Service then, so when the user presses ok or cancel you would either call a bound interface or use SharedPreferences to tell the service about the user's choice.

how to schedule a task, pop up an alert and go to phone home screen?

I want to schedule a task, rise an alert box to notify the user then "quit" my application and automatically go to the phone home screen. But I dont know how to do that. I tried following code but it's not working. May someone help me? Thanks.
timer.schedule(task, calendar.getTime());
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
f.alert(context, title, msg + "Task scheduled for: " calendar.getTime());
The alert method is one I wrote from alertDialog and it's working fine. But no alert is shown when I execute the code. Maybe I'm using the wrong context?
[EDIT]
There is the whole story. I've two scenarios. I allow the user to run the task now or later. If he choose "Now", he get a screen with the progress bar telling him to wait until task is done. Else if he choose "Later" I want to schedule the task with Timer, show an alert or a toast, then go to the home screen. The task waiting background to be executed. So, to skip the progress bar (waiting for right time to run the task), I want to "quit" the application then go to the phone home screen.
You have to show the alert before you start the home activity -- your activity is paused immediately when you call startActivity. You probably want to use a Toast and not an AlertDialog there since you almost certainly don't need any user input at this point; if you are dead set on using an AlertDialog you'll have to either hold off switching to the home activity until they click "OK" on it, or start a new activity with a transparent theme to house the dialog appearing over the home screen.

when user clicks on my notification i want to show an AlertDialog there

how can i show an AlertDialog in my activity, when the notification is clicked in notification area.
plz help....
i'm using multiple status bar notifications each with unique IDS.
I've solved a similar problem by having the notification intent open a new activity with a transparent background. The activity then spawns the AlertDialog.
Put some data to signal that an AlertDialog should be shown into a PendingIntent and put this into yourNotification.contentIntent of your notification. Display the AlertDialog from your Activity in case it receives this Intent.
Have a look at the Notifications Doc.

Categories

Resources