1

Using a WebView on Android for WebSocket communication needs to have the WebView attached. But Android does not allow me to add an invisible (size 0,0) WebView for the whole application lifecycle (in iOS this was easy to solve). Unattached WebView will not allow me WebSocket communication.

(a) I tried this with a modeless Dialog which even can span more Activities and it was able to successfully communicate with the server, but i failed handling void onBackPressed() {...} (no access to handle the backstack for myself) and Android sends this to the modeless Dialog instead of the Activity behind it.

(b) So i created a Service like in: Android: Using WebView outside an Activity context. It can load the html/js and can report from js back to java. But i fail to have WebSocket communication although attached.

My question now: either (a) or (b) ... did anyone solve this? I would really prefer (b) if any possibility.

Community
  • 1
  • 1
ctr
  • 11
  • 3
  • You get any error? Have you enabled javascript and js console logging? – Salem Oct 19 '15 at 08:30
  • it states [INFO:CONSOLE(1193)] "Uncought SyntaxError: Failed to construct 'WebSocket': The URL .... but this perfectly works with the modeless Dialog solution. So i think although the WebView is attached in service with the method shown in the link, that it cannot handle WebSocket. – ctr Oct 19 '15 at 09:12
  • Why don't you use a native WebSocket client library? Is there any reason you have to use WebView for WebSocket communication? – Takahiko Kawasaki Oct 21 '15 at 09:50
  • had netty, ning, commons-httpclient, etc ... as none of them work out of the box (add them in source -> flood of errors), i adopted them (same code for android and ios) until i had a "running" version. mostly ok, but often open/close/open/close at beginning. in 99% of all cases established stable connection with server after some cycles, seldom on first. after tomcat->jetty switch on server, it stopped working. so i switched to webview solution. on ios it was running perfectly after 1 day. on android i do not know how to attach a modeless dialog throughout the whole lifecycle or in a service? – ctr Oct 24 '15 at 07:37
  • Did you found a solution for that? – Michael Burger Aug 21 '17 at 14:06

0 Answers0