1

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

  • I need to use JavaScript and manage cookies, therefore easiest way to have these available is to use some kind of a GUI-less browser.
  • There are no GUI-less web browser available for Android, so I have to find a way to use WebView or find a way to emulate a browser.
  • I want to use a WebView instance which can only run on an UI thread.

My questions are:

  • Is there a way to serialize the context of my activity so I can use this context to create my WebView?
  • How to use Androids ContextWrapper class to emulate an activity?

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

Community
  • 1
  • 1
jazzcool
  • 113
  • 2
  • 10

1 Answers1

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hi, I'm using a slightly modified (trimmed to work without alarmreciever) version of your `WakefulIntentService`. I also created a `SyncBrowse` class that I initialize with an instance of `WebView`, which handles the asynchronous behavior of the `WebView`. This class waits until my JS is executed or the page-load is complete using a `JavaScriptInferance` and `.wait()` `.notify()`. Should I be concerned that I make my WakefulIntentService wait so long? FYI this app will not be used by large masses, just couple of people for couple of hours per day with ~1hr inbetween ServiceAlarms. – jazzcool Aug 07 '15 at 14:06
  • 1
    @cozkul: "Should I be concerned that I make my WakefulIntentService wait so long?" -- that is not what `WakefulIntentService`, or `IntentService` itself, was designed for. Use a regular `Service`, so you can control the threads and the service lifetime. – CommonsWare Aug 07 '15 at 14:13
  • Hi again. I moved everything over to a `Service`. It is not possible to initialize a `WebView` with a `Service` context. When I declare a `static public Context` in one of my activities and use this to initialize my `WebView` at least it initializes, but still when you give webView.loadUrl() command the app crashes. Do you have any idea to make this work? – jazzcool Aug 07 '15 at 21:20
  • @cozkul: "It is not possible to initialize a WebView with a Service context" -- as I wrote, it used to work. However, it has been quite some time since I tried it. What specific problem did you encounter? 'Do you have any idea to make this work?" -- I suggest that you open a fresh Stack Overflow question, where you provide your code, your stack traces, etc. – CommonsWare Aug 07 '15 at 21:23
  • Ok thanks. I'll create a new question with better explanation and provide code tomorrow. I found out that when you use a static context everything works but you must call loadUrl on the main UI thread. – jazzcool Aug 07 '15 at 21:29