7

I know this has been asked a lot of times in StackOverflow already, but I haven't quite found a solution yet. My app sends an email with a link in it that when clicked should launch the app.

According to @hackbod, the best way to do it is to make use of the Intent URI (see this). Here's my code that sets the intent and puts it in the email body:

Intent customIntent = new Intent(CUSTOM_ACTION);
customIntent.setPackage(MY_PACKAGE);
customIntent.addCategory(MY_CAT_BROWSABLE);
customIntent.addCategory(MY_CAT_DEFAULT);

String customUri = customIntent.toUri(Intent.URI_INTENT_SCHEME);

String emailBody = getString(R.string.intent_link, customUri);

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, "Recommending vid");
intent.putExtra(Intent.EXTRA_TEXT   , Html.fromHtml(emailBody));
try {
    startActivity(Intent.createChooser(intent, "Choose email client:"));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}

This is what I get from LogCat:

08-25 17:01:23.333: VERBOSE/Test URI(16987): intent:#Intent;action=com.test.project.action.VIEW_VID_FROM_LINK;category=android.intent.category.DEFAULT;category=android.intent.category.BROWSABLE;package=com.test.project;end
08-25 17:01:23.338: VERBOSE/Test email text(16987): Hi,<br><br>Testing intents from an email.<br><br> A standard website: <a href=http://www.google.com>Go to Google</a>.<br><br> This link should launch the app: <a href=intent:#Intent;action=com.test.project.action.VIEW_VID_FROM_LINK;category=android.intent.category.DEFAULT;category=android.intent.category.BROWSABLE;package=com.test.project;end>Click link to launch</a>.

When I view the email from my phone using the GMail app, I can click on the Google link and it launches the browser, no problem with that.

But the link for the intent is not even clickable (While from the draft it looks like it should be clickable). Has anybody tried this and made it work?

From draft: link looks clickableSent email: link not clickable

EDIT #1: I have also tried setting the action to Intent.ACTION_VIEW but the link is still not clickable.

EDIT #2: Apparently, the link really is clickable. I tried using another email client, and the links are clickable! Seems like there is a bug in GMail. Oh well. But apparently, this is harder than I thought. I tried using:

Uri.Builder builder = new Uri.Builder();
builder.scheme("my.own.scheme");
builder.authority("my.authority");
Uri newUri = builder.build();
Intent customIntent = new Intent(CUSTOM_ACTION, newUri);

As suggested by @CommonsWare, I tried checking if there are receivers of this customIntent. Apparently there is one, which is what I was expecting. So next step is to make this intent into a URI that I can use in the email. I used:

String customUri = customIntent.toUri(Intent.URI_INTENT_SCHEME);

which, based on my understanding of the documentation, should give me something like the usual http links, only with the scheme set to intent. I can then use this customUri as the value for the links in the email. BUT, it looks like this is not the case. Does anybody have an example of what .toUri must return?

Community
  • 1
  • 1
Zarah
  • 5,179
  • 2
  • 29
  • 29
  • This is getting long, but I think I found the problem. This is the Intent I want the link to spawn: `Intent { act=com.test.project.action.CUSTOM_ACTION dat=com.test.project://my.authority }`. Instead, I am getting this Intent when I click on the link in the email: `Intent { act=android.intent.action.VIEW dat=intent://my.authority#Intent;scheme=com.test.project;action=com.test.project.action.CUSTOM_ACTION;end (has extras) }`. Based on the Intent.parseUri(String uri, int flags) documentation, the email client is interpreting my URI as *NOT* generated by toUri(). Is my assumption correct? – Zarah Aug 25 '10 at 14:26
  • were you able to get this to work and what was the solution? Thanks! – hopia Sep 19 '12 at 04:51
  • @hopia I haven't tried this again in a long time. You can look at the comments between me and CommonsWare below to look at my findings during investigation. You can also try out the newer answer below (haven't tried it yet). :) – Zarah Sep 19 '12 at 05:09
  • Yes, I just tried the suggestions and none seem to work. I'm beginning to think intent: scheme is not supported by the android browser. I can actually start the activity from the command line via adb shell am start "intent:#Intent;action=...." – hopia Sep 19 '12 at 21:34

2 Answers2

4

You could try quotes around your URLs in your <a> elements, since that is how HTML is supposed to be written.

You might also try confirming, via parseUri(), PackageManager, and queryIntentActivities(), if your generated URL resolves to something -- if it does not, then there is a problem with the URL.

Here is a sample project showing the use of URI_INTENT_SCHEME, in case it gives you any ideas.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
3

Use zero flag to create URI with your custom scheme. Intent.URI_INTENT_SCHEME flag forces "intent:" scheme (and overall intent URI crazy syntax) to be used.

String customUri = customIntent.toUri(0);
lexis
  • 31
  • 1