Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 998
Akhil Soman Avatar asked Nov 11 '19 11:11

Akhil Soman


People also ask

Can I open android app from URL?

In this case, use the android:path attribute or its pathPattern or pathPrefix variants to differentiate which activity the system should open for different URI paths. Include the BROWSABLE category. It is required in order for the intent filter to be accessible from a web browser.


2 Answers

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

like image 160
Vinay W Avatar answered Sep 30 '22 09:09

Vinay W


In Kotlin, try using makeMainSelectorActivity :

val defaultBrowser = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_BROWSER)
defaultBrowser.data = Uri.parse("https://yoururl.com/")
startActivity(defaultBrowser)
like image 37
Jéwôm' Avatar answered Sep 30 '22 08:09

Jéwôm'