Web视图网址转到浏览器。如何避免并在同一网页视图中打开?

时间:2015-04-13 09:20:37

标签: android webview

这里我的代码......我已经实现了 WebViewClient concept.it在网页视图中效果很好..在我的应用程序中有内容通过fb,twitter,g + ,,, fb也可以登录并打开在webview中...但是当我在我的应用程序中点击共享到fb图标之后,所有进程都可以在浏览器上运行..但我只想在webview中查看所有进程...

OnCreate中

webView = (WebView) findViewById(R.id.webView1);
webView.setClickable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebClient());

if (Build.VERSION.SDK_INT < 8) {
     ...
} else {
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
}

webView.loadUrl("www.example.com.");

WebViewClient

public class myWebClient extends WebViewClient {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    public void onLoadResource (WebView view, String url) {

    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        webView.loadUrl("file:///android_asset/myerrorpage.html");
    }

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        progress.setVisibility(View.VISIBLE);
        view.loadUrl(url);
        return true;
    }

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

2 个答案:

答案 0 :(得分:1)

您必须覆盖您在WebViewClient类中的 shouldOverrideUrlLoading()方法并遵循以下代码

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (Uri.parse(url).getHost().equals("www.example.com")) {
        // This is my web site, so do not override; let my WebView load the page
        return false;
    }
    // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(intent);
    return true;
}

浏览the documentation了解详情。

答案 1 :(得分:0)

在shouldOverrideUrlLoading方法中使用if子句返回false; 来自Building web apps in web views

的示例

if (Uri.parse(url).getHost().equals("www.example.com")) { // This is my web site, so do not override; let my WebView load the page return false; }