android custom url scheme..?
Asked Answered
H

3

46

Im trying to create my own url scheme so my android app can get called via an URL but for now I dont have a success.

Im trying to have this url to work : cedemo://com.cedemo.scan?X=toto

Here is part of my manifest file :

<activity android:name=".Gallery1" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.GALLERY" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="cedemo" android:host="com.cedemo.scan" />

        </intent-filter>
</activity>

Does anyone can help telling me what is wrong ? Also, if someone find what is wrong, can someone tell me how I read the "X" variable from inside the android code from my app ?


Update:

Update: I did the modification of the action (as advised in one of the answers) and it's worked fine. The thing is that I still cannot get the url variable value. Here is the code I tried.

final Intent intent = getIntent();
final String myScheme=intent.getScheme();
final Bundle myBundle=intent.getExtras();
final boolean inContestKey;
if (myBundle != null) {
    inContestKey=myBundle.containsKey("inContest");
}
final Uri myURI=intent.getData();
final String value;
if (myURI != null) {
    value = myURI.getQueryParameter("inContest");
}

But I receiving null from all the functions… what else can i do?

May be I should explain better the context of my software:

  1. My software is started
  2. My software launch then the browser
  3. the user click a link in the browser and the browser go to the url scheme, back to the software with a variable "X" (for example)
  4. the software should read the variable "X"

But in my case : myScheme, myBundle, myURI are set to null.

Any ideas ?


Update:

I found the answer is that you have to be in the main activity to do that.

Hydrozoan answered 26/10, 2010 at 11:43 Comment(0)
S
46

I think the problem is with the Action you defined. There is a "android.intent.action.VIEW" which is what I think you want.

<activity android:name=".Gallery1" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="portrait">
        <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:scheme="cedemo" android:host="com.cedemo.scan" />

        </intent-filter>
</activity>

Try that and I bet will resolve correctly. I only made this assumption because you included the browsable category which is usually used by the Browser, which does not know of any of your custom actions. If you do want the GALLERY action as you have implied then just create 2 filters

<activity android:name=".Gallery1" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.GALLERY" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="cedemo" android:host="com.cedemo.scan" />

        </intent-filter>
        <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:scheme="cedemo" android:host="com.cedemo.scan" />

        </intent-filter>
</activity>

So within the contents of your activity you can do something like:

// Value should be "toto" as in your example
String value = getData().getQueryParameter("X"); 
Singhal answered 26/10, 2010 at 19:12 Comment(7)
HI, THANK YOU SO MUCH for the infos. I changed the ACTION to action.VIEW and its worked fine. The only thing is that I am not able to catch the url variable "X"... I tried this code :Hydrozoan
public void onStart(){ super.onStart(); final Intent intent = getIntent(); final String myScheme=intent.getScheme(); final Bundle myBundle=intent.getExtras(); final boolean inContestKey; if(myBundle !=null) inContestKey=myBundle.containsKey("inContest"); final Uri myURI=intent.getData(); final String value; if(myURI!=null) value = myURI.getQueryParameter("X"); but everything is receiving NULL value... What can i do ??Hydrozoan
This could be a bunch of things. I think you might be better asking a separate question so that you can display a clearer context. However my assumption is that you have an activity that is started in SingleTop mode and after it is started it sends the user to the browser, and then the browser sends it back. However the getIntent() returns the original Intent that started the activity the first time, since it was just pulled to the front. The old intent that was used didn't have data so all is null. Try overriding onNewIntent(). However like I said, this might be better as a separate question.Singhal
The BROWSABLE category did it for me. Good catch!Antichlor
Important note: manually typing the urlscheme into the browser address bar and hitting enter to load it does not work. It DOES work if you CLICK a URL with that urlscheme!Stiletto
@Greg Giacovelli In the <intent-filter> in your answer you list 'android:launchMode="singleTask"' but in the comment above you mention "...my assumption is that you have an activity that is started in SingleTop mode..." Can you clarify? Would you use both but in different situations? Or is only one recommended?Barrera
@Barrera The Singletop was mentioned as a response to OP's question within the comments. It's a secondary question as to why the intent was coming back null. But I will be honest it was years ago and I can't really read code formatted in the comments block of SO.Singhal
C
8

What finally fixed things for me was changing the order of the XML elements. Specifically, swapping the data and action rows so data is before action made it start working.

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

And for completeness, my link in the html is "myappname://noop".

Cush answered 11/7, 2013 at 16:24 Comment(1)
Just tested this, the order made no difference for me.Stiletto
T
3

One more thing, i would like to throw light. Initially it was not working for me because i was using Main/Launcher tags before View/Default/Browsable/data.

Once i changed the order it started working fine. i.e Initially not working code

<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"/>
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="myapp" />
</intent-filter>

correct code order

 <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:scheme="myapp" />
</intent-filter>

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>

and way to test for android is very simple. Make any html file insert

<a href="myapp://http://www.google.com/">test to launch myapp</a> <br /><br />

just open this html file and click on test to .... :-)

Traveller answered 5/12, 2014 at 6:23 Comment(1)
Just tested this, the order made no difference for me.Stiletto

© 2022 - 2024 — McMap. All rights reserved.