启动使用意图的URI我的应用程序 [英] Launching my app using the intent URI

查看:434
本文介绍了启动使用意图的URI我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已经被问过很多在StackOverflow的时候了,但我还没有完全找到一个解决办法。我的应用程序发送电子邮件,它是当单击要启动应用程序的链接。

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.

据@hackbod,做到这一点的最好办法就是要利用意向URI(见)。这是我的code,设置的目的,并把它在电子邮件正文:

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();
}

这是我从LogCat中获得:

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

当我认为从我的手机使用Gmail应用程序的电子邮件,我可以点击谷歌链接,它会启动浏览器,有没有问题。

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?

编辑#1:我自己也尝试设置行动Intent.ACTION_VIEW但链接依然无法点击

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

编辑#2:很显然,该链接确实的的点击。我尝试使用其他电子邮件客户端,链接点击!好像有在GMail中的一个错误。好吧。但很显然,这是比我想象的更难。我尝试使用:

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

所建议的@CommonsWare,我想检查是否有这个接收器 customIntent 。显然,有一个,这正是我所期待的。所以,接下来的步骤是让这个意图成为一个URI,我可以在电子邮件中使用。我使用的:

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

其中,基于我的文档的理解,应该给我像通常的HTTP链接,只能用计划设置为意图。那么我可以用这个 customUri 为在电子邮件中的链接的价值。但,它看起来像这样情况并非如此。没有任何人有什么 .toUri 必须返回一个例子吗?

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?

推荐答案

您可以尝试在你的网址的报价在你的&LT; A&GT; 元素,因为这是多么HTML是应该被写入。

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

您也可以尝试确认,通过 parseUri() PackageManager queryIntentActivities (),如果生成的URL解析到的东西 - 如果没有,再有就是与URL问题

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.

下面是一个示例项目显示使用 URI_INTENT_SCHEME ,如果它给你的任何想法。

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

这篇关于启动使用意图的URI我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆