开发者

Android - Activity from a service?

I am extending the IntentService class to run a background service on Andro开发者_如何学编程id. 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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜