简体   繁体   English

单击android Webview中的链接时启动打开浏览器

[英]Launching a open a browser when clicking a link in android webview

I'm trying to make a link inside my android webview load into one of the web browsers available onto the device. 我正在尝试将我的android webview内的链接加载到设备上可用的一种网络浏览器中。 What is happening is that it just loads the link inside the webview. 发生的是,它只是将链接加载到了webview中。 How can I make the link load outside the webview of my application? 如何使链接加载到应用程序的Webview之外?

The link that I am need to make work is this: 我需要工作的链接是这样的:

<a href="http://www.testsite.com/linka">Forgot your password?</a>

When I click it nothing happens, I need it to call the default web browser of the device. 当我单击它时,什么也没有发生,我需要它来调用设备的默认Web浏览器。

My shouldOverrideUrlLoading is currently being used to check the URL and loads another activity if it is equal: 我的shouldOverrideUrlLoading当前用于检查URL并加载另一个相等的活动:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    Log.d("extraData", "the urls"+Uri.parse(url).getPath());
    if (Uri.parse(url).getPath().equals("/qr_code")) {
        progress.setVisibility(View.GONE);
          Intent intent = new Intent(getApplicationContext(),QRActivity.class);               
        startActivityForResult(intent, 1);
      setContentView(R.layout.activity_qr);        

        //return true;
    }
        if(Uri.parse(url).getPath().equals("/linka")){
            return true;
        }

    return false;
}

Update: It is now working but for some reason my current webView still runs the link therefore the pages loads to the link clicked 更新:现在可以正常工作了,但是由于某种原因,我当前的webView仍然运行该链接,因此页面加载到了单击的链接上

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.d("extraData", "the urls"+Uri.parse(url).getPath());
        if (Uri.parse(url).getPath().equals("/TMMobile/Main/qr_code")) {
            //return true;
        }
        if(Uri.parse(url).getPath().equals("/linka")){
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://testdomain/RecoverPassword.aspx"));
            startActivity(browserIntent);
        }
        return false;
    }
}

To open a URL in a browser, just can use the following code: 要在浏览器中打开URL,只需使用以下代码即可:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM