如何在WebView中强制打开URL?将App设置为WebView URL的默认值 [英] How to force open URL in WebView? Set App as Default for 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
.
我希望默认情况下在WebView中打开URL.
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.
如何解决此问题?
这篇关于如何在WebView中强制打开URL?将App设置为WebView URL的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!