【问题标题】:Open Link from Browser in Android Webview在 Android Webview 中从浏览器打开链接
【发布时间】:2014-03-12 15:12:03
【问题描述】:

我想在带有 webview 的 Android 应用程序中使用主机 example.comexample.de 在浏览器中打开链接。
我创建了这个意图:

<intent-filter>
    <data android:scheme="https" android:host="example.com" />
    <data android:scheme="https" android:host="example.de" />
    <data android:scheme="http" android:host="example.com" />
    <data android:scheme="http" android:host="example.de" />

    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

默认情况下,WebView 加载 URL example.com,这是我的 onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_main);

    final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...", true);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    // Stop local links and redirects from opening in browser instead of WebView
    mWebView.setWebViewClient(new MyAppWebViewClient());

    mWebView.setWebViewClient(new WebViewClient() {

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

        @Override
        public void onPageFinished(WebView view, String url) {
            if (pd.isShowing() && pd != null) {
                pd.dismiss();
            }
        }
    });

    mWebView.loadUrl(url);
}

这是我的 shouldOverrideUrlLoading():

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if(Uri.parse(url).getHost().endsWith("example.com") || Uri.parse(url).getHost().endsWith("example.de") ) {
            return false;
        }

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }

现在我被困住了。如果您在 Web 浏览器中单击 URL example.com/otherurl.php,应用程序将打开,但会加载默认 URL example.com。如何打开应用程序并加载 example.com/otherurl.php 而不是 example.com

我已经读过here,我需要这段代码来获取url:

Uri data = getIntent().getData();
String extUrl = data.toString();

但是我应该在哪里实现这个代码呢? 谢谢

【问题讨论】:

    标签: android android-intent webview


    【解决方案1】:

    extUrl 将包含要打开的 url。所以你应该在调用 loadUrl() 的地方做这样的事情:

    Uri data = getIntent().getData();
    
    if (data != null) {
        mWebView.loadUrl(data.toString());
    } else {
        mWebView.loadUrl(url);
    }
    

    所以你在这里说的是,如果浏览器向你发送了 url,数据将不会为空,因此使用该地址启动 webview。否则,如果 data 不包含地址并且为空,则使用您的默认地址(url 变量)启动。

    【讨论】:

    • 嗯,这太简单了^^我以为我已经尝试过了......非常感谢,这工作完美!
    猜你喜欢
    • 2011-07-26
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 2014-07-12
    • 2018-02-11
    相关资源
    最近更新 更多