Launching custom Android application from Android browser / Chrome
Asked Answered
O

3

33

First of all, my question is extremely similar to this, this and this. The Android documentation for what I'm trying to achieve is here. I couldn't get this to work using these resources so please don't mark this as duplicate as it is not.

I have a website and an Android application. The user will be able to scan QR codes that contain links like http://mywebsite.com/map/. When the user tries to open this link, I want Android to show him a chooser dialog where he can choose to open that link using my application. If my application is not installed, it should proceed to the specified website.

I know Chrome allows this by opening the chooser dialog when the user navigates to that address. For example, try downloading the Stack Exchange app and going to this question in Chrome. It will show this:

Screenshot of chooser dialog.

I have added the following code in AndroidManifest.xml after following the suggestion in the above-mentioned answers:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.SEND" />

    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

    <data
        android:host="mywebsite.com"
        android:path="/map"
        android:scheme="http" />
    <data
        android:host="mywebsite.com"
        android:path="/animals"
        android:scheme="http" />
    <data
        android:host="mywebsite.com"
        android:path="/articles"
        android:scheme="http" />
</intent-filter>

Also, I have tried adding android:mimeType="text/plain" to data but it didn't help.

The problem is that when I go to http://mywebsite.com/map or http://mywebsite.com/map/ Chrome just opens the webpage without showing the chooser dialog.

I would like to mention:

  • following the Android documentation, I have added this code inside one of the activity structures in AndroidManifest.xml. As I am not sure this is the perfect place to add it, I have also tried adding it outside the application structure and directly inside the application structure but it didn't work
  • this is the only code I have implemented for this to work. If something else is needed please let me know. From what I understand, adding a href to the webpage is only needed when using custom schemas
  • I do not want to use a custom schema in order to achieve this
  • I am developing on a Nexus 4, running Android 4.4.2 (latest)
Oskar answered 9/2, 2014 at 18:20 Comment(5)
If your device has the AOSP Browser app, try that. Not all browsers necessarily use ACTION_VIEW for http URLs.Karynkaryo
I am aware of that as for example Dolphin does not show the chooser when going to StackExchange addresses. I have tried using AOSP as well and it doesn't open the chooser either.Oskar
For those struggling to make it work, refer to this answer: #30605749Ormuz
Have you tried opening up the mimeType="*/*"?Stemware
how can i do for this exam.com/9491192536Julianajuliane
P
66

You need to set it up like this :

<intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="example.com"
            android:pathPrefix="/someresource/"
            android:scheme="http" />
        <data
            android:host="www.example.com"
            android:pathPrefix="/someresource/"
            android:scheme="http" />
    </intent-filter>

Notice that in your case, you would need to use android:pathPrefix instead of android:path.

Photozincography answered 12/2, 2014 at 11:45 Comment(6)
This is the correct answer! It would be great for people who will see this answer in the future if you could elaborate on why this is needed. Please note SO will only allow me to award the bounty in 9 hours.Oskar
I added this code my manifest file, but chooser dialog is not appering when i try to open my URL on android web browser. Please help me.Thanks in advance. <intent-filter> <action android:name="android.intent.action.VIEW"></action> <category android:name="android.intent.category.DEFAULT"></category> <category android:name="android.intent.category.BROWSABLE"></category> <data android:scheme="http" android:host="www.google.com"></data> </intent-filter>Meatball
try adding android:pathPrefix="/" as in example and be sure that u have version without www alsoPhotozincography
This has to be highlighted on every answer on this topic that surprisingly misses the pathPrefix element and still seems to work for a lot of users. Just doesn't work without it on chrome. Thanks a ton.Microprint
What can i do when user has selected the browser/chrome as default app to open these kind of urls? It wont show the dialog box...Warenne
I am still unable to open my app from browser even after using pathprefix. please helpKatushka
F
6

Just to be sure, you should reset the preferences for your app in case you have accidentally set it to always open the link in chrome rather than show the chooser dialog. Once "Always" is used to open the matching uri, it will never show the chooser.

Second, you can have as many data elements in the intent filter as your want, but it is not necessary to repeat information. You can do the same thing like this:

<data android:host="mywebsite.com"/>
<data android:scheme="http"/>
<data android:path="/map"/>
<data android:path="/animals"/>
<data android:path="/articles"/>

But note that for the path, you can just use a wildcard

<data android:path="/.*"/>

Consider adding an additional

<data android:host="www.mywebsite.com"/>

And finally you may not want to show the chooser dialog but open a specific app intent/activity directly. On your website, if you detect the android user agent, you can create a link url this way:

<a href="intent://mywebsite.com/articles#Intent;package=com.myapp;scheme=http;end;"/>

See here for more details How do I open any app from my web browser (Chrome) in Android? What do I have to do with the A Href link?

Note that with this method if the app is not installed the user will be taken to the Google Play store for the specified app package.

If you are still having problems, check your intent filter priority. http://developer.android.com/guide/topics/manifest/intent-filter-element.html

Fazeli answered 12/2, 2014 at 6:43 Comment(0)
B
3

In my case site URL is: http://www.example.org/mobile/

so putting these code into AndroidManifest.xml inside activity

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="www.example.org"
                    android:pathPrefix="/mobile/"
                    android:scheme="http" />
            </intent-filter>

Here,

scheme -> Protocol of particular site

host-> Exact site url with WWW

pathprefix - > Your site's sub path if available

Now,

You can search with chrome / etc android browser'search box like example then open chosen dialog ..!!

Blount answered 7/9, 2015 at 7:23 Comment(1)
@war_Hero Did you fnd any solution for this?Topeka

© 2022 - 2024 — McMap. All rights reserved.