在普通浏览器中从Android Webview打开链接作为弹出窗口

Mar*_*jnG 13 javascript java android webview

所以我所拥有的基本上是一个加载页面的简单webview.此页面包含一些在webview中打开的链接.这就是它应该做的,所以它一切正常.

但是该页面中只有一个链接应该作为弹出窗口加载,所以我希望它在人们点击时在普通浏览器中打开.但正如我所说,所有链接都在webview中打开,因此链接也是如此.

我的问题是,如何在普通浏览器中打开此链接作为一种弹出窗口?它甚至可能吗?链接是可变的,所以它总是在变化,它不能在应用程序中硬编码以在新的浏览器浏览器中打开.

有人能告诉我它是否可能以及如何做到这一点?非常感谢你!

Blu*_*ell 28

以下是覆盖webview加载以保留在您的webview中或离开的示例:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class TestWebViewActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        WebView webView = (WebView) findViewById(R.id.webview);
        webView.setWebViewClient(new MyWebViewClient());
    }
}


class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.contains("somePartOfYourUniqueUrl")){ // Could be cleverer and use a regex
            return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
        } else {
            view.loadUrl(url); // Stay within this webview and load url
            return true;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

public class WebViewActivity extends Activity {

    private WebView webView;
    private ProgressDialog progress;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        WebView myWebView = (WebView) findViewById(R.id.webView1);
        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.loadUrl("https://www.example.com");
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("https://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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)