Open Android app from URL using intent-filter not working
Asked Answered
S

4

38

I have an Android app that people use as a replacement for a website. Hence, when users encounter an URL to the website, I want to give them the option to "open the URL" in my app instead of in the browser. In other words I want the popup to appear that lets them choose between my app and the browser (and possibly other apps).

I understand from various sources that I need to add an intent filter to an activity in my app with the 'data' filter that filters on URLs of the correct form.

The website in question is http://members.iracing.com, hence I have added the following intent filter:

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="http" />
            <data android:host="members.iracing.com" />
        </intent-filter>
    </activity>

I have tried various forms of these data filters, like using a single 'data' node with both attributes:

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="http" android:host="members.iracing.com"/>
        </intent-filter>

It is simply not working. I don't know what else to tell you. I hosted a simple HTML page on my website with a couple links to various pages on that website (all starting with "http://members.iracing.com/...") and when I click any of them, they simply open in the browser without ever asking me which app I want to use. I tried it both on the emulator as well as after installing the app on my physical device, nothing works. I tried this in a completely BLANK, new Android project just to see if that would work, nothing.

I then realized that the website requires authentication, and if you are not logged in it redirects to the login page at https://members.iracing.com/membersite/login.jsp, hence I tried replacing the scheme by "https". I even tried changing the host to "www.members.iracing.com", and in the end I even tried a combination of all these things (not sure if this should work, but hey, I'm desperate at this point.....)

       <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:host="members.iracing.com" />
            <data android:host="www.members.iracing.com" />
        </intent-filter>

Still no go. I'm not sure if the redirect is relevant though, the browser clearly first goes to the non-redirected site, then does the redirect to the login page, but at no point do I get the choice to open it in my app. Furthermore, if I login manually in the browser first, there is no redirect, and it still does not work.

Am I missing something obvious here? I'm pulling my hair out why this isn't working, and I cannot debug it besides trying every possible combination I could think of (I did...). Thanks for any help!

Scabby answered 8/11, 2012 at 13:14 Comment(3)
developer.android.com/guide/components/intents-filters.html => i think you may need a mime-type and an authority. #2959201 => you may need to remove the categoryLamia
also, in the application managing stuff in the settings, check if your browser is not selected as default app for http stuffLamia
Note that there is now an in-built editor available for Android Studio developer.android.com/studio/write/app-link-indexing.htmlUnderthecounter
V
37

use these three in your <intent filter>

<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
Valverde answered 8/11, 2012 at 13:31 Comment(4)
Are these the only things needed within the intent filter to make it work because I am trying this with twitter.com on my emulator but it doesn't launch the twitter appAlthaalthea
Wouldn't this intercept ANY website?Cordilleras
In order to specify the particular site you would be required to add the following <data android:host="callback" android:scheme="x-oauthflow-schema" /> along with the above tagValverde
@Althaalthea you can't use this to start some other app. The OP is trying to start his own app.Valerie
C
66

I thought I will post this here since I spent some time looking into why my intent filter did not work. It turns out that it was working all along but matches are exact. Thus if you register your intent for http://myexample.com and you click in http://myexample.com/blah it will not work.

Adding the following fixed the issue:

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

So the intent filter looks like:

<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" />
            <data android:scheme="https" />
            <data android:pathPattern="/.*" />
</intent-filter>
Challis answered 20/8, 2013 at 23:14 Comment(0)
V
37

use these three in your <intent filter>

<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
Valverde answered 8/11, 2012 at 13:31 Comment(4)
Are these the only things needed within the intent filter to make it work because I am trying this with twitter.com on my emulator but it doesn't launch the twitter appAlthaalthea
Wouldn't this intercept ANY website?Cordilleras
In order to specify the particular site you would be required to add the following <data android:host="callback" android:scheme="x-oauthflow-schema" /> along with the above tagValverde
@Althaalthea you can't use this to start some other app. The OP is trying to start his own app.Valerie
G
10

@nurieta, your answer did the trick for me thanks!

One word of advice since I am dealing with potential query strings, I handled it through a bit of code in the .java in order to determine if to just open the app or to send you to a specific location in the app. My app is just a WebView that runs a JQuery mobile app.

    if(getIntent().getData()!=null){//check if intent is not null
        Uri data = getIntent().getData();//set a variable for the Intent
        String scheme = data.getScheme();//get the scheme (http,https)
        String fullPath = data.getEncodedSchemeSpecificPart();//get the full path -scheme - fragments

        combine = scheme+"://"+fullPath; //combine to get a full URI
    }

    String url = null;//declare variable to hold final URL
    if(combine!=null){//if combine variable is not empty then navigate to that full path
        url = combine;
    }
    else{//else open main page
        url = "http://www.example.com";
    }
    webView.load(url);
Geld answered 13/8, 2014 at 20:35 Comment(3)
shouldn't it be scheme+":"+fullPath; ?Deadhead
Code works really well for me. I had to remove the if statements because I ran into an issue where it would always be nullAdvantage
More easy would be just getIntent().getData().toString()Typewriter
V
0

I know this is an old question, but given its popularity....

On Android 12+ this won't work unless you either use Verified App Links or your users set your app as their "Default Browser".

Valerie answered 26/6 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.