jazzcool
jazzcool

Reputation: 183

How to use WebView in an IntentService?

I'm in the same situation with this guy. I need to use a WebView in an IntentService. To summarize:

My questions are:

Any other thoughts that are not mentioned here? Some food for thought: Link1

Upvotes: 1

Views: 826

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007296

There are no GUI-less web browser available for Android

While I have not tried it recently, WebView at least used to have no particular requirement for displaying its UI. I used WebView from within a Service, in my case for a JavaScript interpreter (back before we had better options for that).

Now, an IntentService is not a good choice. WebView is largely asynchronous, and IntentService will destroy itself before WebView gets a chance to do its work. Use a regular Service, where you control the Service lifetime, so you can call stopSelf() only when you are ready to do so.

I want to use a WebView instance which can only run on an UI thread.

The relationship between WebView and threads is complicated. But, back when I last tried it, IIRC, a WebView that is not actually appearing on the screen did not need the main application thread. But, as I noted, WebView does most of its work asynchronously. You may find that you do not need a background thread of your own.

Is there a way to serialize the context of my activity so I can use this context to create my WebView?

No, nor would it address any of your concerns.

How to use Androids ContextWrapper class to emulate an activity?

That is not possible, nor would it address any of your concerns.

Upvotes: 2

Related Questions