views:

698

answers:

2

So I want to create an Android app so it would be registered somewhere in android OS (or just would start on system start) and when phone user clicks on special button on a web page inside a web browser a la:

 <a href="myapp://mysettings">Foo</a> 

my app would pop up and run using the params sent in that URL.

So how do I do such thing?

I need a tutorial with code!

+7  A: 

You need to follow the standard rules for URIs via the W3C and such, which basically means: do not do this.

Android defines a Uri syntax for describing a generic Intent. There are methods on Intent for converting to and from this representation, such as: http://developer.android.com/reference/android/content/Intent.html#toUri(int)

So the way to do this is to use the normal facilities to describe an in your manifest for the kinds of intents you are going to handle with a particular component, especially defining an action name in your own namespace (com.mycompany.myapp.action.DO_SOMETHING or whatever). You can then make an Intent that matches your component, and use Intent.toUri() to get the URI representation of this. This can be placed in your link, and will then when pressed look for something that handles and and thus find your app. Note to be launched from the browser like this, the component's must handle the BROWSABLE category. (You don't need to have this in the Intent you put in the link, the browser will automatically add this in for you.)

Finally, you may want to set the package of the intent to your app with this: http://developer.android.com/reference/android/content/Intent.html#setPackage(java.lang.String)

This is a newer feature in the platform, which allows you to direct link intents to only your app so that other applications can not intercept and handle them.

In summary: read the regular documentation on intents and intent filters (such as the NotePad tutorial, though you won't be using content: URIs here, probably just custom actions) and get your app working that way. Then you can make a browser link to launch your app in the same way, provided your intent-filter handles the BROWSABLE category.

hackbod
At first I thought that you didn't explain how ignoring the standard rules for URIs is bad, but then I found your [other answer here](http://stackoverflow.com/questions/2448213/how-to-implement-my-very-own-uri-schema-on-android/2449500#2449500).
Casebash
Those URLS don't quite appear properly (the bracket on the end is excluded)
Casebash
I can't get this to work. Are the URIs meant to be of the following form? `intent:#Intent;action=android.intent.action.VIEW;component=com.mycompany.project/.InitActivity;end`
Casebash
@hackbod: Like Casebash, I am having problems with your instructions. Given an activity, I dump `getIntent().toUri(Intent.URI_INTENT_SCHEME).toString()`, paste that in a Web page (http://commonsware.com/sample), try opening it in a browser on the emulator, and I get choices of opening the page in Browser, Contacts, or Phone, but not my activity. That seems strange, considering that the `intent:` URI from `toUri()` has the `component` clause, so I'm not sure how those other apps can get it. I have the `BROWSABLE` category on the intent filter. If you have any thoughts, please @ me back. Thanks!
CommonsWare
The browser strips the component part. Because that allows you to launch any component (that you have permission for) regardless of what else is in the intent, it would be a security risk to blindly launch such a link.If you want to launch a specific activity in an .apk, starting in 2.1 you can set a package on an intent, which limits its matching to the activities in that package. So you can build an intent with action="com.mycompany.myaction" and package="com.mycompant"; your package's activity that supporting the "com.mycompany.myaction" action and the DEFAULT and BROWSABLE catergories.
hackbod