scheme host not working on android lollipop, click on link to open app
Asked Answered
M

5

17

I am using this piece of code to launch my app from a link.

<activity
        android:name="com.example.myApp.myClass"
        android:label="@string/app_name" >
    <intent-filter>
        <data
            android:host="customHostName"
            android:scheme="customScheme" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>  

This is href link, i want to get the key in the end.

customScheme://customHost/49FYTJTF00

It is working fine on all previous versions of android, but is not working on Lollipop.
When I click the link it only shows the list of browsers to launch.

What should I do?

Margaritamargarite answered 18/3, 2015 at 7:44 Comment(1)
Can you please share your url. include your prefix path.. try to chnge sdk 21 aswellZamarripa
K
24

Edit:

After testing and testing, I found that if your scheme contains an uppercase character the browser won't be able to launch it. Your scheme should only contain lowercase characters!

Also note that bug 459156 of the chromium project still doesn't allow you to launch url's directly, you should reference users to a webpage containing this JavaScript code:

<!DOCTYPE html>
<html>
    <body>
        <script language="javascript">
            window.location = 'customscheme://customHost/49FYTJTF00';
        </script>
    </body>
</html>

Users landing on this page will automatically be prompted with either an Activity chooser dialog or directly send to your Activity.

To try it, open the Android browser go to the url below and copy paste the above snippet in the editor: http://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro

Gif showing browser + JavaScript opening the Activity

Gif

Original post

I tried out your custom URI and it works on Android 5.0

But you should be aware of the following two bugs/issues:

  1. Bug 459156 of the Chromium project This basicly means launching custom schemes from the Android browser does not work unless the URL is applied using JavaScript. For a workaround see this StackOverflow post: OAuth and custom scheme result in a "ERR_UNKNOWN_URL_SCHEME" in Chrome
  2. Bug 80971: URI with custom scheme are not clickable in SMS Text (Linkify?)

My approach

I created two separate Activities, one as intent receiver and the other as intent launcher. The launching activity has an EditText where the full URI can be entered and a button to launch the entered URI.

Main.java onClick (Launching activity)

@Override
public void onClick(View view) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(input.getText().toString()));
    try {
        startActivity(intent);
    } catch(ActivityNotFoundException e){
        Toast.makeText(this, "Auww!! No Activity can open this URI!", Toast.LENGTH_SHORT).show();
    }
}

manifest.xml (only the activities)

Note the <data android:pathPattern=".*"/> part. this part is important so anything after the host will be accepted as valid.

<activity
    android:name=".Main"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

<activity
    android:name=".UriActivity"
    android:label="@string/app_name">

    <!--
    This intent-filter will open any URI with the following forms:
    
    customscheme://customHost/49FYTJTF00
    customscheme://customHost
    https://www.google.com/something/something
    http://www.google.com/
    http://google.com/something
    -->

    <intent-filter>
        <data android:scheme="https"/>
        <data android:scheme="http"/>
        <data android:host="google.com"/>
        <data android:host="www.google.com"/>

        <data android:scheme="customscheme"/>
        <data android:host="customHost"/>

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

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

UriActivity.java (Receiving activity)

public class UriActivity extends ActionBarActivity {

    private TextView tvText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uri);
        tvText = (TextView) findViewById(R.id.text);
        setTextFromIntent();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setTextFromIntent();
    }

    private void setTextFromIntent(){
        StringBuilder text = new StringBuilder();

        Uri data = getIntent().getData();
        if(data != null){
            text.append("Path:\n");
            text.append(data.getPath());

            text.append("\n\nScheme:\n");
            text.append(data.getScheme());

            text.append("\n\nHost:\n");
            text.append(data.getHost());

            text.append("\n\nPath segments:\n");
            text.append(Arrays.toString(data.getPathSegments().toArray()));
        } else {
            text.append("Uri is null");
        }
        tvText.setText(text);
    }
}

Screenshot:

Result of sending the custom intent to the UriActivity

Test project download:

I made a download for the project if you wan't to try it out yourself.

Kitsch answered 14/4, 2015 at 10:14 Comment(13)
Thanks @Rolf but It works when scheme is http or https but i am using custom scheme ... But if i use http/https as scheme and click the link from email app, it shows open with dialog or opens up the default app chosen to launch urls i.e. browser. I want it to open my app.Margaritamargarite
@Margaritamargarite a custom URI does work for me (see screenshot). Have you read the bug report about the chromium browser? Custom schemes are not supported unless loaded using window.location (JavaScript)Sinker
You can download the project and remove the google.com and http/ https parts and try it for yourself ;)Sinker
@Margaritamargarite I just tried to launch the custom scheme from the Android 5.0 browser using JavaScript and I found that ti didn't work, how ever when I use a lowercase scheme everything works fine! So you should use a lowercase scheme.Sinker
Just don't forget the JavaScript part ;) I also updated the downloadable source code!Sinker
i am sending myself hyperlink as an email, hfi://elabelrid/123, and i don't know why still it didn't worked :/ ?Margaritamargarite
and it did worked on previous versions of android :/ Tried on 4.4Margaritamargarite
And @Rolf i am opening it from email appMargaritamargarite
Does my sample project work for you? Did you edit the schemes in the manifest.xml file? Note: The email app is probably using a WebView to display mail content. A WebView in Android 5.0 uses Chromium webkit, because of the bug I mentioned clicking on the link does not work. What if you try to send a JavaScript link?Sinker
Well that's your problem than, I can't help you with that ;) Or you should wait for the Chromium bug fix.Sinker
i figured out that your suggested method works on few browsers(i.e. dolphin, UC Browser) but not on chrome, and neither on some apps... No Activity can open this URIMargaritamargarite
You could fill in a bug report at the chrome developers.Sinker
Thanks Rolf. how to manager this as pathPattern example?id=123 <data android:pathPattern=""/> i tried example?id= and example?id=* but not working! any idea?Lalitta
Z
5

Please use pathprefix.

   android:pathPrefix="/"

It might solve your problem. Please follow android developer guide URL.

Zamarripa answered 13/4, 2015 at 12:59 Comment(0)
S
2

Instead of using a link to customScheme://customHost/49FYTJTF00, have you tried using a link like

<a href="intent://customHostName/49FYTJTF00#Intent;scheme=customScheme;end">

Chrome should be able to open that in your app just fine. At the same time, this might not work on all browsers.

Slag answered 18/4, 2015 at 11:47 Comment(0)
R
1

Keep in mind if you want run the app from Google Chrome you should do that in another way .

Rehearing answered 19/4, 2015 at 15:35 Comment(2)
Well its not only about chrome, but any email app the user have or any browser he usesMargaritamargarite
@Margaritamargarite I don't give you solution for all cases, but I'm pretty sure the approach I mentioned will help you with Chrome.Rehearing
I
0

You wrote:

    <data
        android:host="customHostName"
        android:scheme="customScheme" />

So, use customScheme://customHostName/49FYTJTF00
instead of customScheme://customHost/49FYTJTF00

Islamite answered 17/4, 2015 at 9:34 Comment(1)
Thats just a typo in the question (as an example), the OP said that it had worked before.Sinker

© 2022 - 2024 — McMap. All rights reserved.