Вопрос

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)
Это было полезно?

Решение

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.

Другие советы

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

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 ..!!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top