There are several use cases for allowing your app to handle custom URIs. The first one I’ll cover is simply launching your application.
Launching your application from the web
A common trend is for companies to include a link to download their rich application on its website. One way you could handle a link like this is to launch the rich application if the user already has it installed, or direct them to the market so they can download it.
First, you need to decide what URI you are going to use to achieve this. My natural tendency was to come up with a custom URI that, chances are, only your application will recognize. However, after doing some research, I found a comment from a Google Engineer (or so the comments seem to indicate) on Stack Overflow that strongly recommended against this approach. He states:
“Please DO NOT use your own custom scheme like that!!! URI schemes are a network global namespace. Do you own the “anton:” scheme world-wide? No? Then DON’T use it.”
Instead, he recommends using a URI of a website that you own, and I think this makes a lot of sense. After all, most legitimate apps on the market have a website counterpart, so why not take advantage of it?
Let’s assume your application’s website is http://yourapp.com. I recommend using a “/get/” URI from your website. You’ll want to make your Android application respond to this URI from the web. This is achieved by assigning an intent-filter in your application’s Manifest XML file, as such:
The VIEW action and BROWSABLE category are both needed so your activity can respond to web URIs. The data element is necessary to have the activity respond to the particular URI I mentioned previously, and that element’s attributes should be fairly self-explanatory.
The only thing left to do is to create a landing page such as “/get/index.html” on your site that will redirect the user to the appropriate app store to download your app. The following simple script could be used:
Now that everything is in place, you can create a link that will launch your app or redirect the user to the market if your app is not installed:
Communicating with your app
You could take this technique a step further to communicate data from the web to your application.
Let’s assume you want to be able to have a user click a link on your website that automatically composes an SMS for them on their phone. This time, instead of using the ‘path’ attribute in the data tag of the intent-filter, we can use the pathPrefix attribute.
The pathPrefix attribute will allow your app to respond to links such as:
Next, you can add the following code to the onCreate method of the activity you applied the intent-filter to:
path = getIntent().getData().getPathSegments();
Uri uri = Uri.parse("smsto:" + path.get(1));
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
As you can see from the code above, we start by getting an array of the path segments of the URI that was captured by the intent-filter. In this case, the array would look like this:
["sms", "5554443333", "hello"]
Now it’s just a matter of crafting the intent using the path segments array and launching the intent.
The example I provide in this post is fairly basic, but I hope it will help you apply the same logic to solve your own problem. With rich apps and the web becoming more of a blend every day, I’m sure you will eventually find a use case for this technique.