Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open an Android app via a URL in the browser

Ok, so I know there has to be more to it than this. I've read many answers on here that all say the same thing but it's not working for me. I want to be able to open an Android app using a link with a custom URL scheme from the browser. First, I am using the emulator (actually Genymotion) so I don't know if that has anything to do with it. The answers I see keep saying that all I need is something similiar to this:

<intent-filter>
    <data android:scheme="myapp" android:host="hello"/>
    <action android:name="ANDROID.INTENT.ACTION.VIEW"/>
    <category android:name="ANDROID.INTENT.CATEGORY.BROWSABLE"/>
    <category android:name="ANDROID.INTENT.CATEGORY.DEFAULT"/>
</intent-filter>

I put this on a new blank activity, not the main activity.

If I understand correctly, any URL starting with myapp://hello will open the activity with that intent filter. However, no matter what I try, I keep getting an ERR_UNKNOWN_URL_SCHEME in the browser. This is the stock Android browser as I can't put Chrome on Genymotion (at least not with the free version). Is this something that requires Chrome? Is it something that can only be done on an actual device? Is there something that I am doing wrong?

like image 786
Douglas Starnes Avatar asked Mar 15 '23 21:03

Douglas Starnes


1 Answers

There are several things in play here.

First, as Blackbelt noted, Android is case-sensitive, as are most programming languages and development environments. You would need to change this to be the proper case, following the documentation for those actions and categories:

<intent-filter>
    <data android:scheme="myapp" android:host="hello"/>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

Second, not all Web browsers handle schemes like myapp:// the same. I am not aware of any law on the books in any jurisdiction forcing developers to honor them. There are three basic patterns that you will encounter:

  • Some browsers may handle them just fine

  • Some browsers will handle them when such URLs are used in links on a page, but not if the URL is typed in the address bar

  • Some browsers will not attempt to find an activity to handle them and just fail all the time

Google's preferred alternative is for you to use an http or https scheme, with a host and path that you control. Some browsers will always open the Web page, but others will check, see that there is an activity that advertises support for that URL, and give the user a choice of how to handle it. The "M" version of Android will provide further hooks ("App Links") that can bypass the chooser. Plus, other apps (e.g., text messaging clients, email clients) will typically make http links clickable, allowing users the choice of opening up your app, whereas few apps will know that myapp://hello is meaningful in the same way.

like image 106
CommonsWare Avatar answered Mar 29 '23 02:03

CommonsWare