Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launching my app using the intent URI

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?

like image 575
Zarah Avatar asked Aug 25 '10 09:08

Zarah


People also ask

What is the intent action to launch phone application with number?

Intent callIntent = new Intent(Intent. ACTION_DIAL, number); When your app invokes this intent by calling startActivity() , the Phone app initiates a call to the given phone number.

How pass URI to another activity?

String imagePath = getIntent(). getStringExtra("imagePath"); imageview. setImageURI(Uri. parse(imagePath ));

What is a URI in android?

URI(Uniform resource identifier) as its name suggests is used to identify resource(whether it be a page of text, a video or sound clip, a still or animated image, or a program). The most common form of URI is the Web page address, which is a particular form or subset of URI called a Uniform Resource Locator (URL).

How do I get Android app URI?

An easy way to obtain the proper URI for an Android app is to visit the app's page in the Google Play Store, tap the share button, and paste the copied link somewhere you can read it. The link will look like https://play.google.com/store/apps/details?id=com.twitter.android.


2 Answers

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.

like image 78
CommonsWare Avatar answered Oct 08 '22 17:10

CommonsWare


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);
like image 33
lexis Avatar answered Oct 08 '22 19:10

lexis