Rectangle 27 0

java How to register some URL namespace (myapp:app.start) for accessing your program by calling a URL in browser in Android OS?


<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 intent = getIntent();
// check if this intent is started via custom scheme link
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
  Uri uri = intent.getData();
  // may be some test here with your custom uri
  String var = uri.getQueryParameter("var"); // "str" is set
  String varr = uri.getQueryParameter("varr"); // "string" is set
}

@Proverbio in the onCreate of the activity under which you registered the intent-filter. It's certainly not necessarily the main activity

@Proverbio: in the main activity of your app, within the onCreate method

First, to be able to start your app from link with custom scheme 'myapp' in browser / mail, set intent filter as follows.

The first example I've seen on here that includes how to get the Uri out again in the activity.. thanks!

Where the code that handles the intent should be coded?

if you use custom scheme to launch your app, one of the problem is that: The WebView in another apps may not understand your custom scheme. This could lead to show 404 page for those browser for the link with custom scheme.

Note
Rectangle 27 0

java How to register some URL namespace (myapp:app.start) for accessing your program by calling a URL in browser in Android OS?


04-13 09:47:30.742: DEBUG/ezpz(9098): intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;S.some_variable=123456;end
<body>
    <a href="intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;end">click to load apk</a>
</body>
<intent-filter>
    <action android:name="com.bubblebeats.MY_CUSTOM_ACTION" />
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
Intent i = new Intent();

i.setAction("com.bubblebeats.MY_CUSTOM_ACTION");
i.putExtra("some_variable", "123456");

Log.d("ezpz", i.toUri(Intent.URI_INTENT_SCHEME));
S.some_variable=123456
intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;S.some_variable=123456;end
intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;end

@Jason, were you able to get this to work? Launching the intent via "adb shell am start 'intent:#Intent..." works for me. However, from a browser, clicking on the linnk intent doesn't trigger the app launching.

Create an intent filter in the activity you want to load in the manifest like this:

Looks like the package is appended like package=com.bubblebeats; (Also, while not evident in this example, the package is URI encoded)

Take a good look at what occurred here and you can see how you can skip the code step and manually create these URIs yourself.

This is the best ans i see in all the answers.

What does the package (from intent.setPackage(..) serialize out to?

Where does the S. syntax is documented? Just relying on the output of toUri() doesn't seem a good idea.

You can generate the URI from within your Android code like this:

You just want this part:

Note
Rectangle 27 0

java How to register some URL namespace (myapp:app.start) for accessing your program by calling a URL in browser in Android OS?


@hackbod - It appears this would not work with iOS though. It seems registering a unique scheme is the most cross-platform compatible implementation although I agree with you that it pollutes the namespace... developer.apple.com/library/ios/documentation/iPhone/Conceptual/

@hackbod can you please point me to w3c documentation which suggests to not implement new URI schemes? I have only found rfc2718 which seems to support that.

@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 (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!

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)

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.

Don't bother trying this method if you want your URIs to reliably work; using intent URIs requires cooperation from the app taking the URI. zxing (Barcode Scanner) doesn't accept them (by deliberate design decision, apparently), so if you want to be able to scan barcodes to talk to your app (or take URI input from any other method you don't completely control), you either need to overload http:// (which requires a user choice between browser and your app) or just go with a custom scheme, which as far as I can tell passes transparently into the system and doesn't get caught up anywhere).

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.

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.)

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.

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

Note
Rectangle 27 0

java How to register some URL namespace (myapp:app.start) for accessing your program by calling a URL in browser in Android OS?


<a href="intent:#Intent;action=com.yourpackage.action.OPEN_VIEW;package=com.yourpackage;S.myextra=anystring;end">Open App with extra</a>
<activity
    android:name=".activity.MainActivity"
    android:label="@string/app_name_full">
    <intent-filter>
        <action android:name="com.yourpackage.action.OPEN_VIEW"></action>
        <category android:name="android.intent.category.DEFAULT"></category>
        <category android:name="android.intent.category.BROWSABLE"></category>
    </intent-filter>
</activity>
Intent i = new Intent();
        i.setAction("com.yourpackage.action.OPEN_VIEW");
        i.setPackage("com.yourpackage");
        i.putExtra("myextra","anystring");
        Log.d(getClass().getSimpleName(), i.toUri(Intent.URI_INTENT_SCHEME));
final Intent intent = getIntent();
final String action = intent.getAction();

        if ("com.yourpackage.action.OPEN_VIEW".equals(action)) {
           Log.i(getClass().getSimpleName(), "EXTRA: "+intent.getExtras().getString("myextra"));
        }

First of all, you need to define a custom action in your manifest file:

Hope that helps someone for better understanding. Please correct me, if I got something wrong.

On your website (this is the previously generated link):

Then, for the content on your website, you need to generate the URI from an intent. Put following code in your Activity (This code could be removed, once the link is generated):

To receive the Extras, put following in your activity, that is able to recieve the custom action (as defined in manifest):

Note
Rectangle 27 0

java How to register some URL namespace (myapp:app.start) for accessing your program by calling a URL in browser in Android OS?


@hackbod - It appears this would not work with iOS though. It seems registering a unique scheme is the most cross-platform compatible implementation although I agree with you that it pollutes the namespace... developer.apple.com/library/ios/documentation/iPhone/Conceptual/

@hackbod can you please point me to w3c documentation which suggests to not implement new URI schemes? I have only found rfc2718 which seems to support that.

@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 (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!

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)

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.

Don't bother trying this method if you want your URIs to reliably work; using intent URIs requires cooperation from the app taking the URI. zxing (Barcode Scanner) doesn't accept them (by deliberate design decision, apparently), so if you want to be able to scan barcodes to talk to your app (or take URI input from any other method you don't completely control), you either need to overload http:// (which requires a user choice between browser and your app) or just go with a custom scheme, which as far as I can tell passes transparently into the system and doesn't get caught up anywhere).

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.

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.)

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.

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

Note
Rectangle 27 0

java How to register some URL namespace (myapp:app.start) for accessing your program by calling a URL in browser in Android OS?


<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 intent = getIntent();
// check if this intent is started via custom scheme link
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
  Uri uri = intent.getData();
  // may be some test here with your custom uri
  String var = uri.getQueryParameter("var"); // "str" is set
  String varr = uri.getQueryParameter("varr"); // "string" is set
}

@Proverbio in the onCreate of the activity under which you registered the intent-filter. It's certainly not necessarily the main activity

@Proverbio: in the main activity of your app, within the onCreate method

First, to be able to start your app from link with custom scheme 'myapp' in browser / mail, set intent filter as follows.

The first example I've seen on here that includes how to get the Uri out again in the activity.. thanks!

Where the code that handles the intent should be coded?

if you use custom scheme to launch your app, one of the problem is that: The WebView in another apps may not understand your custom scheme. This could lead to show 404 page for those browser for the link with custom scheme.

Note
Rectangle 27 0

java How to register some URL namespace (myapp:app.start) for accessing your program by calling a URL in browser in Android OS?


04-13 09:47:30.742: DEBUG/ezpz(9098): intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;S.some_variable=123456;end
<body>
    <a href="intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;end">click to load apk</a>
</body>
<intent-filter>
    <action android:name="com.bubblebeats.MY_CUSTOM_ACTION" />
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
Intent i = new Intent();

i.setAction("com.bubblebeats.MY_CUSTOM_ACTION");
i.putExtra("some_variable", "123456");

Log.d("ezpz", i.toUri(Intent.URI_INTENT_SCHEME));
S.some_variable=123456
intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;S.some_variable=123456;end
intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;end

@Jason, were you able to get this to work? Launching the intent via "adb shell am start 'intent:#Intent..." works for me. However, from a browser, clicking on the linnk intent doesn't trigger the app launching.

Create an intent filter in the activity you want to load in the manifest like this:

Looks like the package is appended like package=com.bubblebeats; (Also, while not evident in this example, the package is URI encoded)

Take a good look at what occurred here and you can see how you can skip the code step and manually create these URIs yourself.

This is the best ans i see in all the answers.

What does the package (from intent.setPackage(..) serialize out to?

Where does the S. syntax is documented? Just relying on the output of toUri() doesn't seem a good idea.

You can generate the URI from within your Android code like this:

You just want this part:

Note
Rectangle 27 0

java How to register some URL namespace (myapp:app.start) for accessing your program by calling a URL in browser in Android OS?


@hackbod can you please point me to w3c documentation which suggests to not implement new URI schemes? I have only found rfc2718 which seems to support that.

@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 (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!

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)

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.

Don't bother trying this method if you want your URIs to reliably work; using intent URIs requires cooperation from the app taking the URI. zxing (Barcode Scanner) doesn't accept them (by deliberate design decision, apparently), so if you want to be able to scan barcodes to talk to your app (or take URI input from any other method you don't completely control), you either need to overload http:// (which requires a user choice between browser and your app) or just go with a custom scheme, which as far as I can tell passes transparently into the system and doesn't get caught up anywhere).

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.

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.)

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.

Those URLS don't quite appear properly (the bracket on the end is excluded)

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

Note
Rectangle 27 0

java How to register some URL namespace (myapp:app.start) for accessing your program by calling a URL in browser in Android OS?


@Jason, were you able to get this to work? Launching the intent via "adb shell am start 'intent:#Intent..." works for me. However, from a browser, clicking on the linnk intent doesn't trigger the app launching.

Create an intent filter in the activity you want to load in the manifest like this:

Looks like the package is appended like package=com.bubblebeats; (Also, while not evident in this example, the package is URI encoded)

Take a good look at what occurred here and you can see how you can skip the code step and manually create these URIs yourself.

This is the best ans i see in all the answers.

What does the package (from intent.setPackage(..) serialize out to?

Where does the S. syntax is documented? Just relying on the output of toUri() doesn't seem a good idea.

You can generate the URI from within your Android code like this:

You just want this part:

Note
Rectangle 27 0

java How to register some URL namespace (myapp:app.start) for accessing your program by calling a URL in browser in Android OS?


<a href="intent:#Intent;action=com.yourpackage.action.OPEN_VIEW;package=com.yourpackage;S.myextra=anystring;end">Open App with extra</a>
<activity
    android:name=".activity.MainActivity"
    android:label="@string/app_name_full">
    <intent-filter>
        <action android:name="com.yourpackage.action.OPEN_VIEW"></action>
        <category android:name="android.intent.category.DEFAULT"></category>
        <category android:name="android.intent.category.BROWSABLE"></category>
    </intent-filter>
</activity>
Intent i = new Intent();
        i.setAction("com.yourpackage.action.OPEN_VIEW");
        i.setPackage("com.yourpackage");
        i.putExtra("myextra","anystring");
        Log.d(getClass().getSimpleName(), i.toUri(Intent.URI_INTENT_SCHEME));
final Intent intent = getIntent();
final String action = intent.getAction();

        if ("com.yourpackage.action.OPEN_VIEW".equals(action)) {
           Log.i(getClass().getSimpleName(), "EXTRA: "+intent.getExtras().getString("myextra"));
        }

First of all, you need to define a custom action in your manifest file:

Hope that helps someone for better understanding. Please correct me, if I got something wrong.

On your website (this is the previously generated link):

Then, for the content on your website, you need to generate the URI from an intent. Put following code in your Activity (This code could be removed, once the link is generated):

To receive the Extras, put following in your activity, that is able to recieve the custom action (as defined in manifest):

Note