如何在 WebView 中强制打开 URL?将应用程序设置为 WebView URL 的默认值 [英] How to force open URL in WebView? Set App as Default for WebView URL

查看:35
本文介绍了如何在 WebView 中强制打开 URL?将应用程序设置为 WebView URL 的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过来自浏览器和 Google 搜索的 URL 打开我的 WebView 应用.

I am trying to open my WebView app from URLs from browser and Google Search.

我尝试使用此处建议的以下代码:如何在android中点击url启动应用

I tried using the following code as suggested here: How to launch app on click of url in android

    <intent-filter>
        <data android:scheme="https" />
        <data android:scheme="https" android:host="www.webviewurl.com"/>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

但是没有用.

我想强制打开所有 URL,例如:

I want to force open all URLs like:

https://*.webviewurl.com/*
http://webviewurl.com/*

到WebView中的https://www.webviewurl.com/*.

to https://www.webviewurl.com/* in the WebView.

我已经使用 .htaccess 在服务器中完成了这部分.

I've already done this part in the server using .htaccess.

我希望 URL 默认在 WebView 中打开.

I want the URLs to open in WebView by default.

推荐答案

此问题已通过执行以下操作得到解决:

The issue has been resolved by doing the following:

AndroidManifest.xml

            <intent-filter>
                <data android:scheme="https" />
                <data android:scheme="https" android:host="www.webviewurl.com"/>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>

MainActivity.java

public class webclient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.contains("play.google")){
                view.getContext().startActivity(
                        new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            }
            if ((url.contains("webviewurl.com"))) {
                view.loadUrl(url);
                return true;
            } else {
                view.getContext().startActivity(
                        new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            }
            return true;
        }

哪个工作正常.唯一的问题是,所有页面都在应用程序中打开主页.

Which is working fine. Only problem is, all the pages are opening the homepage in app.

就像如果用户从浏览器点击链接说 https://www.website.com/folder/page,它会打开 https://www.website.com/ 在应用程序中.

Like if an user clicks on a link say https://www.website.com/folder/page from browser, it opens https://www.website.com/ in the app.

我该如何解决这个问题?

How do I fix this issue?

这篇关于如何在 WebView 中强制打开 URL?将应用程序设置为 WebView URL 的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆