简体   繁体   中英

Open Link from Browser in Android Webview

I would like to open links in browsers with the host example.com or example.de in my Android application with webview.
I created this intent:

<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>

By default, the WebView loads the URL example.com , here is my 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);
}

This is my 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;
    }

Now I am stuck. If you click on the URL example.com/otherurl.php in a web browser, the app opens, but loads the default url example.com . How can I open the app and load example.com/otherurl.php instead of example.com ?

I already read here , that I need this piece of code to get the url:

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

But where should I implement this code? Thanks

extUrl will contain the url to open. So you should do something like this where you called loadUrl():

Uri data = getIntent().getData();

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

So what you're saying here is that if the browser sent you the url, data won't be null so launch the webview with that address. Otherwise, if data does not contain an address and is null, launch with your default address (the url variable).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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