如何在 IntentService 中使用 WebView?

How to use WebView in an IntentService?

我和 this guy 的情况一样。我需要在 IntentService 中使用 WebView。总结:

我的问题是:

还有没有提到的其他想法here?一些值得深思的地方: Link1

There are no GUI-less web browser available for Android

虽然我最近没有尝试过,但WebView至少以前对显示它UI没有特别的要求。我在 Service 中使用了 WebView,在我的例子中是 JavaScript 解释器(在我们有更好的选择之前)。

现在,IntentService 不是一个好的选择。 WebView 在很大程度上是异步的,并且 IntentService 会在 WebView 有机会完成它的工作之前自行销毁。使用常规 Service,您可以在其中控制 Service 生命周期,因此您可以仅在准备好时调用 stopSelf()

I want to use a WebView instance which can only run on an UI thread.

WebView和线程的关系比较复杂。但是,当我上次尝试它时,IIRC,一个实际上没有出现在屏幕上的 WebView 不需要主应用程序线程。但是,正如我所指出的,WebView 以异步方式完成大部分工作。您可能会发现您不需要自己的后台线程。

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?

这是不可能的,也不会解决您的任何顾虑。