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?
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?
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? – Continuity