在后台运行 WebView [英] Running WebView in Background

查看:84
本文介绍了在后台运行 WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,它利用 WebView 登录网站,从中提取内容,然后在内容更新时通知用户.我已经获得了 WebView 来获取内容,但是我需要知道如何将 WebView 作为服务运行,以便让它在后台运行.

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.

据我所知,WebViews 必须从 UI 线程进行操作,这让事情变得更加困难.

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?

推荐答案

虽然 WebView 需要在单个线程上操作,但它不一定需要是 UI 线程(除非您想将 WebView 附加到视图层次结构),但是对于所有的 WebView,它必须是同一个线程.

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.

虽然它没有得到明确支持(或经过大量测试),但 WebView 并没有什么特别之处可以阻止您在服务中运行它.WebView 确实在 Context 上调用了几个通常在 Service 中不起作用的方法(如 getTheme()),因此您必须使用 ContextWrapper 来解决这个问题.您还需要手动调用 WebView.layout 以欺骗 WebView 使其认为它具有大小.您可能需要做更多的事情,但没有其他想法.

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.

这篇关于在后台运行 WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆