Android: Using WebView outside an Activity context

前端 未结 10 2119
执念已碎
执念已碎 2020-11-27 03:20

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

  • Since
10条回答
  •  忘掉有多难
    2020-11-27 04:07

    A WebView cannot exist outside of an Activity or Fragment due to it being a UI. However, this means that an Activity is only needed to create the WebView, not handle all its requests.

    If you create the invisible WebView in your main activity and have it accessible from a static context, you should be able to perform tasks in the view in the background from anywhere, since I believe all of WebView's IO is done asynchronously.

    To take away the ick of that global access, you could always launch a Service with a reference to the WebView to do the work you need.

提交回复
热议问题