Show a AlertDialog from a Service

Put your problem here if it does not fit any of the other categories.

Show a AlertDialog from a Service

Postby paolo » Sun Nov 30, 2008 6:09 pm

Hi!
I wrote a small application that started a service. Can the service show a AlertDialog?
I tried but I could not.

Thanks!
paolo
Freshman
Freshman
 
Posts: 4
Joined: Thu Nov 13, 2008 2:30 pm

Top

Postby MrSnowflake » Sun Nov 30, 2008 11:29 pm

A service shouldn't create alertdialogs, Services only should generate Notifications.
However, if an Activity is bond to the service, you could display an AlertDialog though this activity.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby paolo » Sun Nov 30, 2008 11:48 pm

My scenario is: I started the service through the activity, I continue to use the phone and the service show me an alertdialogs. Is this possible? How bond as a service to an activity?
Thanks!
paolo
Freshman
Freshman
 
Posts: 4
Joined: Thu Nov 13, 2008 2:30 pm

Postby modha_chintan » Mon Dec 01, 2008 8:46 am

Hi experts,

I am stuck up with exact same problem, I also want to launch alert dialog box from service at some point of time when task is done. But the thing is my service runs independent of activity (it is not binded to any activity) thats's how i want my service to be running background , I tried creating new thread and tried to launch alertdialogbox through it but wasn't successful.

any help or suggestion would be greatly appreciated.

thanks in advance
modha_chintan
Freshman
Freshman
 
Posts: 3
Joined: Mon Nov 17, 2008 8:09 am

Postby MrSnowflake » Mon Dec 01, 2008 10:50 am

paolo wrote:My scenario is: I started the service through the activity, I continue to use the phone and the service show me an alertdialogs. Is this possible? How bond as a service to an activity?
Thanks!
In you cenario you still should use Notifications. Otherwise users will be bothered with anoying alertdialogs. While with notifications, they can choose when to view the message. Which is fully in compliance with the Desing philosophy.

modha_chintan wrote:Hi experts,

I am stuck up with exact same problem, I also want to launch alert dialog box from service at some point of time when task is done. But the thing is my service runs independent of activity (it is not binded to any activity) thats's how i want my service to be running background , I tried creating new thread and tried to launch alertdialogbox through it but wasn't successful.

any help or suggestion would be greatly appreciated.

thanks in advance
I repeat: you should __NEVER__ open dialogs or windows of anykind from a Service. Use notifications and PendingIntents for this!
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby EveningNewbs » Tue Dec 02, 2008 6:47 pm

Is it possible to open Dialogs or AlertDialogs from a service, though? I understand that in most cases, you should use a notification, but the application I'm writing is intended to block the user; it's a sort of parental control app.

EDIT: Never mind, figured it out.

PS, The best way to make sure someone does something is to tell them that they shouldn't. :P
EveningNewbs
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Nov 17, 2008 4:27 pm

Top

Postby MrSnowflake » Tue Dec 02, 2008 8:21 pm

EveningNewbs wrote:PS, The best way to make sure someone does something is to tell them that they shouldn't. :P
Yeah very true, though I thought it didn't go for semi professional developers :).

Anyway, how did you fix it?
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby EveningNewbs » Tue Dec 02, 2008 8:51 pm

MrSnowflake wrote:Anyway, how did you fix it?

I made an Activity and displayed that instead:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. startActivity(new Intent(MyService.this, ActivityMasqueradingAsAnAlert.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


It wouldn't work without the setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).
EveningNewbs
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Nov 17, 2008 4:27 pm

Postby MrSnowflake » Tue Dec 02, 2008 9:22 pm

That's a nice work around. The NEW_TASK is probably needed, because you can't start an activity from a Service in the same process, I guess.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby EveningNewbs » Tue Dec 02, 2008 10:13 pm

Yeah; it would throw an exception with a little message like, "This will start a new task. Are you sure this is what you want?" if I didn't include the flag.

Now I'm having trouble getting a reference to the service that launched it, though. Doing:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.getBaseContext();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

just returns null, and most of the functions that are there to get the creator of the current Activity return objects of type Activity, not Service.
EveningNewbs
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Nov 17, 2008 4:27 pm

Postby MrSnowflake » Wed Dec 03, 2008 11:05 am

Put the Service in the Extras of the Intent.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby EveningNewbs » Wed Dec 03, 2008 3:24 pm

Ah, thanks. I knew there was an easy way to do it. I was just thinking too hard.
EveningNewbs
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Nov 17, 2008 4:27 pm

Postby MrSnowflake » Wed Dec 03, 2008 3:26 pm

This isn't the way you should do it, but then this whole thread is about doing stuff wrong :D.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby EveningNewbs » Wed Dec 03, 2008 4:22 pm

So, what would be more correct, then? Can I just make the newly created Activity bind to the Service when it starts up and have them communicate like civilized application components? I'm pretty new at this and I'm kind of feeling my way out as I go along, so let me know if I have no idea what I'm talking about.
EveningNewbs
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Nov 17, 2008 4:27 pm

Postby MrSnowflake » Wed Dec 03, 2008 4:27 pm

You should use aidl and Activity.bindService() for this.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top
Next

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 19 guests