3

I try to load HTML5 game from internet and show it in my WebView but when I try to do that I can't load the game, my monitor is black and there is only one line (about 30px high) that is on my screen this is my code

public class BrowserGame extends MenuHandler {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.browsergame);

        WebView browser = (WebView)findViewById(R.id.webGame);
        browser.setFocusableInTouchMode(false);

        browser.setWebViewClient(new WebViewClient(){
            @Override  
            public boolean shouldOverrideUrlLoading(WebView view, String url)  
            {
                view.loadUrl(url);
                return true;  
            }
        });

        Bundle game = getIntent().getExtras();
        String gameURL = game.getString("gameURL");

        browser.loadUrl(gameURL);
    }
}

When I try same link on mobile browser it shows the game.

Thanks.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
Kec
  • 1,435
  • 4
  • 16
  • 21

1 Answers1

3

The Game you try to load/play in your WebView is not a pure HTML5 application, but a simple JavaScript game.

To make this work in your own WebView, you'll need to activate JavaScript for this WebView:

WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

In my test-case, this does not work either. The blue line you see is the loader of the Game. LogCat is reporting an Error for the game.js-script:

WARN/webview(1208): Miss a drag as we are waiting for WebCore's response for touch down.
ERROR/Web Console(1208): TypeError: Result of expression 'event' [undefined] is not an object. at http://m.spacemonsters.co.uk/galactians/game.js:261

This only shows up when using a WebView, Androids standard browser does not throw this error. This Error is thrown when you click on the Game. It's also thrown in Androids standard browser, so it might not be the root of the problem.

Also, there seams to be a difference in the way how the standard Android Browser (which can play the game) and the WebView handle and process JavaScript. There are some workarounds posted here, see if one applies to your situation.


Fun Fact: There is a Bug in the Game which makes it very easy to get a damn high score. You can use the P-key to pause the game. In pause mode, you can still move the ship and shots are generated (you simply don't see them). After you hit P again, your shots fly out.

This also makes it very easy to avoid being hit by an enemy laser-shot ;)

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • I try this and there is only blue line on center of the screen I can't see game WebView browser = (WebView)findViewById(R.id.webGame); WebSettings webSettings = browser.getSettings(); webSettings.setJavaScriptEnabled(true); – Kec Aug 22 '11 at 21:17
  • I added some research and a possible workaround. Check it out. – Lukas Knuth Aug 22 '11 at 22:57