2

I have a background service running that needs to display an Allow/Deny AlertDialog to the user and get his choice. Roughly, this what I want. In my service, I have a code like this:

void someMethod()
{
    boolean allow = showAllowDenyBox();
    //Some operations based on the value of allow
}

I want to know how to implement the method showAllowDenyBox(). From this thread, I understand that I have to create an Activity and show the AlertDialog in that Activity. However, once the user selects Allow or Deny, how will I report that choice back to the service? Any help on this would be greatly appreciated.

In short, I want an AlertDialog to be spawned from inside a service and that must be synchronous.

Community
  • 1
  • 1
Aswin Parthasarathy
  • 605
  • 1
  • 8
  • 17

1 Answers1

2

You'll have to use the startActivityForResult method to implement this functionality. FOr more information on returning a result check out this SO post.

Basically using this method you inform the Android system that you want to get a result back from the activity you have passed to this method. Setting that result is explained very well in the above post. Do check it out.

Community
  • 1
  • 1
varevarao
  • 2,186
  • 13
  • 26
  • Thanks. But the problem is that in my service, I have to execute the next line of code only after I get the result from the Activity. If I use startActivityForResult, it tries to finish executing the calling code and only then starts the Activity. I cannot use this as I want the result from the Activity to execute the next line of code in the calling code. – Aswin Parthasarathy Nov 04 '12 at 09:38
  • 1
    You could add the next line(s) of code to a function that's executed in the `onActivityResult` method. – varevarao Nov 05 '12 at 05:53