我的 Android 应用程序中有一个 WebView。它加载一个网页,用户可以单击链接以通过 url 方案启动其他应用程序。现在我需要避免这个动作。
例如,当用户在微信中点击正确的深层链接或应用程序链接时,微信只会在其 WebView 中说“抱歉我们无法启动 xxx...”,用户可以在浏览器中打开该页面以跳转到目标应用程序。我也想做同样的事情。
我在SO中看到过类似的问题how-can-i-disable-deeplinks,但没有有用的答案。我也尝试过在
shouldOverrideUrlLoading
或 shouldInterceptRequest
中做一些事情,但都不起作用。这意味着我可以识别 shouldOverrideUrlLoading
中的特殊 url,但覆盖其返回 true 或 false 无法停止启动应用程序,因为深层链接操作不是 WebView 中的覆盖。
url.startsWith("intent://")
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("intent://")) {
// This is a deeplink URL, return true to disable deeplinking
Toast.makeText(requireActivity(), "sorry we can not launch: "+url, Toast.LENGTH_SHORT).show();
return true;
} else {
// This is not a deeplink URL, let the WebView handle it
return super.shouldOverrideUrlLoading(view, url);
}
}
将类似的内容添加到您的
WebViewClient
:
/**
* The shouldOverrideUrlLoading(WebView view, String url) method is deprecated in API 24 and
* the shouldOverrideUrlLoading(WebView view, WebResourceRequest request) method is added in API 24.
* If you are targeting both older versions of Android and compiling with 24+, you need both methods.
* If not, you can delete the deprecated one
*/
@SuppressWarnings("deprecation") // Needs SDK 24 to be removed
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return handleUrl(Uri.parse(url));
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return handleUrl(request.getUrl());
}
private boolean handleUrl(Uri uri) {
// This can become more complex to support other url schemes like ftp
return !uri.getScheme().startsWith("http");
}
解决方案 1:您可以限制 Activity 的 onCreate() 方法内的深层链接
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Uri uri = getIntent().getData();
if (uri != null && !isValidDeepLink(uri)) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.example.com"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finishAndRemoveTask();
}
}
解决方案2:从Android 15及更高版本开始支持允许标签。 https://developer.android.com/about/versions/15/features#expanded-intentfilter
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="astore.com" />
<uri-relative-filter-group>
<data android:pathPrefix="/auth" />
<data android:query="region=na" />
</uri-relative-filter-group>
<uri-relative-filter-group android:allow="false">
<data android:pathPrefix="/auth" />
<data android:query="mobileoptout=true" />
</uri-relative-filter-group>
<uri-relative-filter-group android:allow="false">
<data android:pathPrefix="/auth" />
<data android:fragmentPrefix="faq" />
</uri-relative-filter-group>
</intent-filter>
这会对你有帮助