0

I have an Activity that runs a WebView and inject some JS into a site (which i don't have control) and returns me some data. I need to do the same thing on background, with a service. How can i do this ?

NOTE: I'm already using an IntentService, AlarmManager and BroadcastReceivers. I only wanna know the better way to implement this service (WebView, Http-Client, others..)

Ps.: I think that if i instantiate a WebView 0Wx0 through service would be dirty. Am i wrong ?

Ps: I read a little about HttpClient, but I do not know if it is useful to me.

Thank you !

Victor Milazzo
  • 128
  • 2
  • 13

2 Answers2

0

webView.loadUrl() is asynchronous

so its already on a background thread: it returns immediately and WebView keeps working on a separate thread.

To get a callback for WebView page loading use WebViewClient.onPageFinished(....)

j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • Sorry, i wasnt clear. I actually use onPageFinished in my UI WebView. But i need to do the same work in the background, with a service. – Victor Milazzo Dec 19 '15 at 21:13
0

have your service create a new webview activity. so on some event in the background you can start a webview Activity and do the same thing. Depending on the type of event you can use a intentService otherwise a sticky long running service

j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • I am using a IntentService to do this. But `WebView`s can only be instantiated from UI Thread. I need to do this with IntentService :/ – Victor Milazzo Dec 19 '15 at 21:26