Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running WebView in Background

I am developing an application which utilizes a WebView to sign into a website, pull content from it, then notify the user when content is updated. I have gotten the WebView to get the content however I need to know how I can run the WebView as Service in order to get it to run in the background.

As I understand, WebViews must be manipulated from the UI thread which makes things a whole lot harder.

Any suggestions/workarounds as to how I can get the app to notify the user regardless of whether they have the app open or not?

like image 844
user3274871 Avatar asked May 15 '14 09:05

user3274871


People also ask

How can I make WebView keep a video or audio playing in the background?

Solution. After searching a lot, I found this thread. I did something similar: Just extend WebView and override onWindowVisibilityChanged . This way, the audio continues to play if the screen is locked or another app is opened.

How will you load URL in WebView in background while splash screen is showing?

You can do it without AsyncTask and you can hide the splash screen when the page loaded without a timer. WebViewClient class has a method onPageFinished() which will be called once the page has been loaded. You can make use of it. In you project folder, place your splash screen images with name 'splash_screen.

How can I improve my WebView performance?

I read about how to increase performance of WebView by implementing Caching web resources like JS, CSS and image files. You can also static resources in your native application, and by intercepting the Resource requests you can override the default behaviour of WebView.

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.


1 Answers

While WebViews need to be manipulated on a single thread it doesn't necessarily need to be the UI thread (unless you want to attach the WebView to the view hierarchy), however it needs to be the same thread for all of the WebViews.

While it's not something explicitly supported (or heavily tested) there is nothing special that the WebView does to prevent you from running it in a Service. The WebView does call a couple of methods on the Context that don't normally work in a Service (like getTheme()), so you'd have to work around that with a ContextWrapper. You'll also need to manually call WebView.layout to trick the WebView into thinking it has a size. There might be more stuff you'd need to do, but nothing else comes to mind.

like image 81
marcin.kosiba Avatar answered Nov 10 '22 14:11

marcin.kosiba