0

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.

Jorge
  • 695
  • 2
  • 9
  • 21
  • jorge why you are creating a popup Activity for conformation.you just create a popupwindow instead of creating an Activity if popupwindow fulfill your requirement? – ρяσѕρєя K May 07 '12 at 14:40
  • @imrankhan No. The activity is the pop-up window (dialog box) that has two buttons - OK and Cancel. – Jorge May 07 '12 at 14:43
  • 1
    @Jorge , he's saying that starting an activity is pointless and wasteful. Use an AlertDialog if all you need is an OK/Cancel response. If you're dead set on an activity you need to use startActivityForResult and onActivityResult.. Example: http://rahulonblog.blogspot.com/2010/05/android-startactivityforresult-example.html – dymmeh May 07 '12 at 14:45
  • @dymmeh I can't use startActivityForResult() from Service's context, just startActivity(). I'll try AlertDialog though, then reply the result. Thanks – Jorge May 07 '12 at 14:52
  • possible duplicate of [android alert dialog from service](http://stackoverflow.com/questions/3599563/android-alert-dialog-from-service) – Ron May 07 '12 at 14:57
  • No, the problem is not the type of Activity or dialog. The problem is the result message! I want to return the result to the place the dialog is called. – Jorge May 07 '12 at 15:31

1 Answers1

0

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.

Michael
  • 1,416
  • 1
  • 9
  • 21