views:

604

answers:

2

I am extending the IntentService class to run a background service on Android. The service starts after bootup immediately. I want to pop-up a user input box from this service periodically. So, I tried calling an Activity from the service, but it doesn't help. The dreaded box of Force Close appears and the service dies. Can anybody suggest some methods to call activity from a background service?

Thanks!

+2  A: 

I want to pop-up a user input box from this service periodically.

Please reconsider this plan. Use a Notification. Your users will thank you.

EDIT: To clarify, I am assuming that you are looking to "pop-up a user input box" asynchronously, without some activity of yours in the foreground. If so, that's not a great UI pattern -- users may easily get annoyed when your activity pops up in the middle of, say, them typing a text message.

So, I tried calling an Activity from the service, but it doesn't help. The dreaded box of Force Close appears and the service dies.

Make sure you call startActivity() from the UI thread. If that does not help, as aforlorney suggested, check LogCat via adb logcat, DDMS, or the DDMS perspective in Eclipse to get the Java stack trace of your exception.

CommonsWare
+1 for Mark's point here. I remember reading this in the Android docs, too. 99 times out of 100, you want to pop up a notification that the user can attend to at their leisure.
Klondike
+1  A: 

This is not directly related to the answer (commonsware.com has already given a good overview of how to try to solve that), but just a tip: If you've got a background service that is running code intermittently, you should have the service use the AlarmManager to wake itself up instead of staying on indefinitely. Basically, start the Service, have it create an PendingIntent to call itself, then have it wake itself up when necessary.

Daniel Lew