Reputation: 183
I'm in the same situation with this guy. I need to use a WebView
in an IntentService
. To summarize:
WebView
or find a way to emulate a browser.WebView
instance which can only run on an UI thread.My questions are:
context
of my activity
so I can use this context
to create my WebView
?Any other thoughts that are not mentioned here? Some food for thought: Link1
Upvotes: 1
Views: 826
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