微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 使用意图URI启动我的应用程序

我知道StackOverflow已经多次询问过这个问题,但我还没有找到解决方案.我的应用会发送一封电子邮件,其中包含一个链接,点击后应启动应用.

根据@hackbod的说法,最好的方法是使用Intent URI(参见this).这是我设置意图的代码并将其放入电子邮件正文中:

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必须返回的例子?

解决方法

您可以在< a>中尝试引用您的网址.元素,因为这就是HTML的编写方式.

您也可以尝试通过parseUri(),PackageManager和queryIntentActivities()进行确认,如果生成的URL解析为某些内容 – 如果没有,则会出现URL问题.

Here is a sample project显示了URI_INTENT_SCHEME的使用,以防它给你任何想法.

原文地址:https://www.jb51.cc/android/315643.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐