我知道这个问题在StackOverflow上已经被问了很多次,但我还没有找到一个解决方案。我的应用程序发送一封电子邮件,其中包含一个链接,点击该链接应该启动应用程序。
根据@hackbod的说法,最好的方法是利用Intent URI(请参见此处)。以下是我的代码,它设置了意图并将其放入电子邮件正文中:
当我使用GMail应用程序从手机查看电子邮件时,我可以点击Google链接并启动浏览器,这没有问题。但是,意图的链接甚至都无法点击(尽管从草稿中看起来它应该是可点击的)。有人尝试过这个并使它工作吗?
编辑#1:我还尝试将操作设置为Intent.ACTION_VIEW,但链接仍然无法点击。
编辑#2:显然,链接确实是可点击的。我尝试使用另一个电子邮件客户端,链接是可点击的!似乎GMail存在一个错误。噢好吧。但显然,这比我想象的要困难。我尝试使用:
根据我的文档理解,这应该会给我类似于常规http链接的东西,只是方案设置为“intent”。然后,我可以将此“customUri”用作电子邮件中链接的值。但是,看起来情况并非如此。有人有
根据@hackbod的说法,最好的方法是利用Intent URI(请参见此处)。以下是我的代码,它设置了意图并将其放入电子邮件正文中:
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中获取的信息:
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应用程序从手机查看电子邮件时,我可以点击Google链接并启动浏览器,这没有问题。但是,意图的链接甚至都无法点击(尽管从草稿中看起来它应该是可点击的)。有人尝试过这个并使它工作吗?
编辑#1:我还尝试将操作设置为Intent.ACTION_VIEW,但链接仍然无法点击。
编辑#2:显然,链接确实是可点击的。我尝试使用另一个电子邮件客户端,链接是可点击的!似乎GMail存在一个错误。噢好吧。但显然,这比我想象的要困难。我尝试使用:
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。 我使用了:
String customUri = customIntent.toUri(Intent.URI_INTENT_SCHEME);
根据我的文档理解,这应该会给我类似于常规http链接的东西,只是方案设置为“intent”。然后,我可以将此“customUri”用作电子邮件中链接的值。但是,看起来情况并非如此。有人有
.toUri
必须返回的示例吗?
Intent { act=com.test.project.action.CUSTOM_ACTION dat=com.test.project://my.authority }
。但是,当我点击邮件中的链接时,我得到的 Intent 是这样的: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) }
。根据 Intent.parseUri(String uri, int flags) 文档,邮件客户端解释我的 URI 不是由 toUri() 生成的。我的假设正确吗? - Zarah