Android应用程序链接-在浏览器中从应用程序打开URL而不触发应用程序链接 [英] Android App link - Open a url from app in browser without triggering App Link
问题描述
我已在我的应用程序中启用了应用程序链接。它工作正常。但是在我的应用程序中,有些情况下我无法处理传入的URL。在那种情况下,我想将该URL重定向到设备中的默认浏览器。
I have enabled App linking in my application. It works fine. But in my application there are some scenarios where i cannot handle the incoming url. In those cases i want to redirect that url to the default browser in the device.
当前我尝试做的是使用意图用URL打开浏览器,但它再次重定向到我的应用程序本身。应用链接的格式为->
Currently what i have tried doing is using intents to open browser with the url, but it again redirects to my app itself. The app link is of the format ->
https://<domain>/<prefix>/<params>
因此,根据参数,我想处理应用本身中的应用链接或重定向它到默认浏览器。以下是我尝试使用上述网址打开浏览器的代码
so depending on the params, i would like to either handle the app link in the app itself or redirect it to the default browser. Below is the code i tried to open the browser with the above url
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(appLinkModel.url))
browserIntent.addCategory(Intent.CATEGORY_APP_BROWSER)
browserIntent.resolveActivity(packageManager)?.let {
startActivity(browserIntent)
}
我尝试排除addCategory()行,但结果相同。应用程序崩溃(因此resolveActivity()),或者应用程序在循环中自行打开。
I tried excluding the addCategory() line but the results are the same. Either the app crashes(hence the resolveActivity()), or app opens itself in a loop.
我想做什么
所以我想做的是重定向默认浏览器的网址(或在其中显示选择器 WITHOUT ),而不会一次又一次触发应用链接。那有可能吗?
So what i want to do is redirect the url to the default browser(or show a chooser WITHOUT my app in it), without triggering the app link again and again. So is this possible?
推荐答案
String data = "example.com/your_url?param=some_param"
Intent defaultBrowser = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_BROWSER);
defaultBrowser.setData(data);
startActivity(defaultBrowser);
此技术(使用makeMainSelectorActivity)将强制链接在设备的默认浏览器中打开
this technique (using makeMainSelectorActivity) will force the link to open in the device's default browser
注意- makeMainSelectorActivity 仅适用于15级及以上的API。
Note - makeMainSelectorActivity only works for API level 15 and above.
如果您需要支持低于15级的API,您可以尝试此黑客
If you need to support API levels lower than 15, you could try this hack
这篇关于Android应用程序链接-在浏览器中从应用程序打开URL而不触发应用程序链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!