Android:在 Activity 上下文之外使用 WebView [英] Android: Using WebView outside an Activity context

查看:27
本文介绍了Android:在 Activity 上下文之外使用 WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过后台 IntentService 实现网页抓取,该后台 IntentService 会定期抓取网站,而不会在用户手机上显示视图.

I am trying to achieve Web Scraping through a background IntentService that periodically scrape a website without a view displaying on the users phone.

  • 因为我必须在加载的页面上调用一些 javascript,所以我不能使用任何 HttpGet 等.
  • 因此,我必须使用只能在 UI 线程上运行的 WebView 实例.
  • 任何尝试启动使用 WebView 的 Activity 都会导致 View 进入手机前台(根据 Android 的活动设计)
  • 任何在 Activity 上下文之外使用 WebView 的尝试都会导致错误,指出您无法在非 UI 线程上使用 WebView.
  • 出于各种复杂性原因,我无法考虑使用 Rhino 等库进行无 UI 网页抓取.

有什么办法可以解决这个问题吗?

Is there any way of working around this problem?

推荐答案

如果我错了,请纠正我,但这个问题的正确答案是,当用户正在做其他事情时,没有可能的方法在后台使用 WebView在不通过 Activity 打扰用户的情况下在手机上进行操作.

Correct me if I am wrong but the correct answer to this question is that there is NO possible way to use a WebView in the background while the user is doing other things on the phone without interrupting the user by means of an Activity.

我已经应用了 Randy 和 Code_Yoga 的建议:使用带有Theme.NoDisplay"的活动来启动带有 WebView 的后台服务来做一些工作.然而,即使没有视图可见,切换到该活动以启动服务的那一秒也会中断用户(例如暂停正在玩的正在运行的游戏).

I have applied both Randy and Code_Yoga's suggestions: Using an activity with "Theme.NoDisplay" to launch a background service with a WebView to do some work. However even though no view is visible the switching to that activity for that second to start the services interrupts the user (ex. like pausing a running game that was being played).

这对我的应用来说是个灾难性的消息,所以我仍然希望有人能给我一种方法来使用不需要 Activity 的 WebView(或可以实现相同功能的 WebView 的替代品)

Totally disastrous news for my app so I am still hoping someone will give me a way to use a WebView that does not need an Activity (or a substitute for a WebView that can accomplish the same)

这篇关于Android:在 Activity 上下文之外使用 WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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