Android App link - Open a url from app in browser without triggering App Link

爷,独闯天下 提交于 2020-01-13 22:39:33

问题


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.

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

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.

WHAT I WANT TO DO

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?


回答1:


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

this technique (using makeMainSelectorActivity) will force the link to open in the device's default browser

Note - makeMainSelectorActivity only works for API level 15 and above.

If you need to support API levels lower than 15, you could try this hack




回答2:


        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://stackoverflow.com/questions/58800240/android-app-link-open-a-url-from-app-in-browser-without-triggering-app-link"));
        Intent chooseIntent = Intent.createChooser(intent, "Choose from below");
        startActivity(chooseIntent);

This will open all the installed web browsers from which user can choose!




回答3:


Unfortunately, we can't exclude any certain URL, since Android doesn't provide that option.

But you can handle using path prefix.

// To handle:
http://myhost.com/v/page1?id=123
// To exclude:
http://myhost.com/v/page2?id=123

then in manifiest

<data android:scheme="http"
      android:host="myhost.com"
      android:pathPrefix="/v/page1" />


来源:https://stackoverflow.com/questions/58800240/android-app-link-open-a-url-from-app-in-browser-without-triggering-app-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!