开发者

Creating An In-Game Web Browser on Android

开发者 https://www.devze.com 2023-02-06 10:22 出处:网络
In my game, I\'d like to have a kind of \"message of the day\" feature. Basically, when the user taps the \"message of the day\" button in the main menu, it would o开发者_开发问答pen up a browser view

In my game, I'd like to have a kind of "message of the day" feature. Basically, when the user taps the "message of the day" button in the main menu, it would o开发者_开发问答pen up a browser view in-game. When the user is done, he taps a "close" button and the view disappears, returning him to the game menu.

So, is it possible to create a browser view dynamically? If yes, how?


The following can be used in a Dialog as well

    public void setWebView() {
//WebViewClient is used if you want to capture stuff from the webview, like if a link was pressed
                WebViewClient yourWebClient = new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        open_web = true;
                        if (!url.startsWith("http://") && !url.startsWith("https://")) {
                                url = "http://" + url;
                        }
                        Intent browserIntent = new Intent("android.intent.action.VIEW",
                                Uri.parse(url));
                        startActivity(browserIntent);
                        return true;
                    }
                };
                WebView wv = (WebView) findViewById(R.id.webview);
                wv.setWebViewClient(yourWebClient);
                String str = getHtml();
                wv.loadData(str, "text/html", "utf-8");
                wv.setBackgroundColor(0);
    }

    boolean isAsset = false;

    private String getHtml(){
        InputStream source = null;
        if (isAsset){
            source = getAssets().open("motd.html");
        } else {
            source = new FileInputStream(Environment.getExternalStorageDirectory() + "motd.html");
        }
    }


You could probably just use a WebView. When you want it to show up you could just set its view state to View.VISIBLE. When you want it to go away, just set its view state to View.GONE.


You can add/remove a WebView to your game dynamically (or show/hide it, whatever you prefer).

Take a look at the WebView tutorial for more info:

http://developer.android.com/resources/tutorials/views/hello-webview.html

Make sure to leave room for your "Close" button in your layout, i.e. you don't want to set the layout height to "fill_parent".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号