Handling URL scheme on Android?

Regarding the Browser not launching your app, it works for me if I use an HTML <a/> tag on my webpage.

Are you invoking your URL via JavaScript instead?

I ask because I know there is this “quirk” with Android’s WebView Java class where it’s WebViewClient.shouldOverrideUrlLoading() does not get called for URLs invoked via JavaScript… and that’s the method that most native Android developers use to override URL handling.

That did the trick.

I was just trying to launch the app directly from the browser or email app, but putting the scheme on a html web page worked.

We have an app where users can share content by emailing a url scheme with a download code in the launchargs, 

so the app will launch and start downloading the content when tapping the url scheme link.

On iOS it is possible to launch the app directly from the mail program, but apparently on android you need to bridge it

using a webpage… but thats not really a problem. 

Thank you Joshua. This was very helpful.

By the way if anyone else is trying to do this kind of thing.

Here is a nice tutorial explaining everything: http://fokkezb.nl/2013/09/20/url-schemes-for-ios-and-android-2/

Thanks for sharing!

Out of interest, what issues were you having with a “singleTask” activity?

The only odd quirk that I’m aware of is if you display an activity from a singleTask activity, home out, and then go back to your app… then Android automatically destroys all of the activities displayed by that task and goes back to the singleTask activity.

I think in this forum I will get exact answer which I want

as Joshua has explained how to use URLScheme in android I m having one problem in this i want to open my application named “try”. what changes I have to do in it.

Thanks in advance 

What you need to do is set up your “try” app with an intent-filter as I’ve shown up above.  You can either give it a custom URL scheme or a the domain of your website.  You would then launch your “try” app from your other app via Corona’s system.openURL() function.  So, for the example I provided above which uses a “corona://” custom URL scheme, you would launch it like this…

   system.openURL(“corona://test”)

Of course, you should use “corona” as your custom URL scheme.  It should be unique to your app.

That said, I actually recommend that you do it by your website’s domain name instead in case the app you’re trying to launch is not installed.  That way it’ll launch the device’s browser app instead and show a web page on your site telling the end-user to download your other app.  Something along those lines.  You would do this via the “data” tag’s “host” attribute instead of using the scheme “attribute” as documented by Google here…

   http://developer.android.com/guide/topics/manifest/data-element.html

if I have to open my app than URL will be in which form

system.openURL(“corona://try”)

or

system.openURL(“try://”).

And I want this URLScheme in ios also

You can set up your own “try://” custom URL scheme in the “build.settings” file as follows…

settings = { android = { intentFilters = { { label = "Your Title Goes Here", actions = { "android.intent.action.VIEW" }, categories = { "android.intent.category.DEFAULT", "android.intent.category.BROWSABLE", }, data = { scheme = "try" }, }, }, }, }

And then you can open your “try” app from your other app using the following function call…

system.openURL("try://your\_arguments\_go\_here")

Thnks Joshua for your fast reply

If you have set up the URL handler like that, you can even launch it from the stock browser or Chrome. I have used it in several applications for some time now and it works entirely as expected. Thanks for making it possible.

I can launch one app from another with the method described here, but it does not seem to work if I want to launch the app from a browser or the email system. Is that supported?

Thanks

Are you sure you have the category “android.intent.category.BROWSABLE” added to your filter, because this is required so that browser and e-mail will consider the intent-filter.

Yes.

I just copied the build.settings code above and changed my scheme name.

It works between apps but not in email and browser.

Does that work for you?

Hmm, I thought it did, but upon new try right now, it didn’t. Sorry, I had that wrongly in mind, because I only use it for bouncing between different apps, and I was pretty sure I also tested it with the settings, but I seem to have recalled that wrongly.

It works in the default Browser app for me.  But it might not work for all 3rd party browser apps… or 3rd party apps in general.  This is because it’s up to the 3rd party app to turn the URL into an Android “intent”, query if there are any activities on the device that support that URL scheme via PackageManager.queryIntentActivities() in Java, and then launching that activity.  If the app does not do this and simply attempt to do an HTTP connection with your custom URL scheme, then of course it will fail.  There is nothing you can do within your app to force another app to handle your custom URL scheme.  Not even if you’ve coded your Android app natively.

So, this is why I’ve suggested up above to use your website’s domain instead of a custom URL scheme.  That way, if the app doesn’t handle custom URL schemes or if your app is not installed, then at least it will take the end-user to your website.  You would do this by setting the “host” part of the intent-filter as described here…

   http://developer.android.com/guide/topics/manifest/data-element.html

Thanks Joshua. That makes sense.

But the strange thing is that I tested it with the default browser and as a link in the default email app and none of these seems to work. I would think that when calling system.openURL(“myapp://“) it also calls the default browser to launch the app right?

When you call system.openURL(“myapp://”), then what we do is turn that URL into an Android intent and then have the Android OS launch the default activity for that URL by calling the Java Activity.startActivity() method…

   http://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent

The above is how you can get your app to launch another app via it’s intent-filter.  If no app on the device has a matching intent-filter for that URL, then the OS defaults to the Browser app.

Now, if the app blindly passes your URL to the Java WebView.loadUrl() method on Android, then it will ignore all app intent-filters on the device and attempt to do an HTTP request to a server that doesn’t exist.  In your case, you’ll get a 404 error.

   http://developer.android.com/reference/android/webkit/WebView.html#loadUrl(java.lang.String

Regarding the Browser not launching your app, it works for me if I use an HTML <a/> tag on my webpage.

Are you invoking your URL via JavaScript instead?

I ask because I know there is this “quirk” with Android’s WebView Java class where it’s WebViewClient.shouldOverrideUrlLoading() does not get called for URLs invoked via JavaScript… and that’s the method that most native Android developers use to override URL handling.

That did the trick.

I was just trying to launch the app directly from the browser or email app, but putting the scheme on a html web page worked.

We have an app where users can share content by emailing a url scheme with a download code in the launchargs, 

so the app will launch and start downloading the content when tapping the url scheme link.

On iOS it is possible to launch the app directly from the mail program, but apparently on android you need to bridge it

using a webpage… but thats not really a problem. 

Thank you Joshua. This was very helpful.

By the way if anyone else is trying to do this kind of thing.

Here is a nice tutorial explaining everything: http://fokkezb.nl/2013/09/20/url-schemes-for-ios-and-android-2/