Running WebView in the background

I am developing an application that uses WebView to enter a site, pull content from it, and then notify the user when the content is updated. I got a WebView to receive content, but I need to know how I can start WebView as a Service to make it work in the background.

As I understand it, WebViews need to be manipulated from the UI thread, which makes things a lot more complicated.

Any suggestions / workarounds regarding how I can get the application to notify the user whether the application is open or not?

+6
source share
3 answers

While WebViews need to be manipulated in a single thread, it does not have to be a UI thread (unless you want to attach a WebView to a hierarchy of views), but it must be the same thread for all WebViews.

Although this is clearly not supported (or heavily tested), there is nothing special that WebView does so that you do not run it in a service. WebView does indeed call several methods in a context that usually do not work in the service (e.g. getThememe ()), so you have to get around this with ContextWrapper. You also need to manually call WebView.layout to trick WebView into thinking that it has size. There may be more things that you will need to do, but nothing comes to mind.

+6
source

Do you mean this?

 runOnUiThread(new Runnable(){ public void run() { ... something } }); 
+2
source

You cannot inflate or alter any view from the background thread. But if you want the contents of a web page, you can get it in the background stream using an HTTP GET. You need to use GET on the url to get its HTML data.

+1
source

All Articles