开发者

How to create a simple android browser?

I simply want to create an apk that will take a url, and open a window and simply run like a browser

so far I have:

public class Browser extends Activity {

    WebView mWebView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.google.com");  
    } 
}

This works fine, except when i open a link it'll take me to the actual browser, I'm having trouble where to place this code to override links opening in a new browser:

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLo开发者_运维知识库ading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}


WebView has a setWebViewClient method.

so you'd do something like

mWebView.setWebViewClient(new HelloWebViewClient());


Check out my project called FriarFramework, which is an ebook app publisher.

It basically takes a collection of HTML files locally and packages them up into a WebView.

https://github.com/hanchang/Friar-Framework


Try this

webView.setWebViewClient(new WebViewClient() {

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

        @Override
        public void onPageFinished(WebView view, String url) {
            progressBar.setVisibility(View.GONE);

        }
    });


mWebView.setWebViewClient(new HelloWebViewClient());

This should be enough.

for more see the official doc about Building web apps in WebView


You have to implement WebViewClient.

You can detect URL inside shouldOverrideUrlLoading() method:

browser.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // You can have URL here } });

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜