Open an Android app via a URL in the browser
Asked Answered
A

4

7

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?

Aksoyn answered 30/5, 2015 at 13:1 Comment(3)
ANDROID.INTENT.ACTION and ANDROID.INTENT.CATEGORY have to be lower caseAdamite
Ok, that was it. I feel stupid for not realizing that in the first place. However, if you start typing the last part in all caps (like 'VIEW') Android Studio apparently inserts the whole string in caps even though the code completion show it in lower case. Thanks!Aksoyn
yes, it a bug of the IDE. you are welcomeAdamite
T
5

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.

Thole answered 30/5, 2015 at 13:26 Comment(1)
I see. Good advice. I've done this on iOS in the past and since all web browsers have to use Apple's UIWebView at some point, they can ensure a consistent API and therefore custom URL schemes will work the same. Thanks!Aksoyn
D
2

if url is http://www.example.com/test/test1 means use the following code:

add in android manifest file

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Holo.Light.NoActionBar">
    <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter android:label="@string/app_name" >
         <action android:name="android.intent.action.VIEW" />
         <!-- Accepts URIs that begin with "http://example.com/test/test1" -->
         <data
             android:host="www.example.com"
             android:pathPrefix="/test/test1"
             android:scheme="http" />
         <category android:name="android.intent.category.DEFAULT" />
         <category android:name="android.intent.category.BROWSABLE" />
     </intent-filter>
</activity>
Defazio answered 30/5, 2015 at 13:24 Comment(0)
S
0

Check out the question I asked here. You should view the answer I awarded bounty to and also view the comments.

Summary:
There is problem with the apps using the android web-view.
The problem is not at your end, but in the apps from where you click the link. In this case your browser.

You can try the HTML Viewer and it will work, atlease it did for me.
Some default email apps i.e. of Xperia did worked.

And now in Google IO 2015 with release of Android M they also have improved app deep linking(Same thing) as we(developers) complained a lot about these.
You can see it here and here.

I hope it helps :)

Sterner answered 30/5, 2015 at 13:30 Comment(0)
M
0

The problem with this is there are no standards - so every solution will have its pitfalls.

After a lot of research and work, I recommend using Android App Links (http/s links) everywhere with a fallback to a page with a deep link using a custom app scheme (myapp://...).

The fallback can be handled with a proxy server - if the App Link doesn't work it will open the browser for a webpage with the opened URL.

It was tested and proven to catch all known pitfalls.

BTW: Most popular browsers today handle custom app schemes perfectly on Android.

Monoxide answered 9/3, 2021 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.