【发布时间】:2017-11-06 23:45:26
【问题描述】:
我想在 Youtube 应用中注册一个专门用于分享的意图过滤器。
到目前为止,我能够成功接收来自 Youtube 的意图。问题是我的意图文件管理器不够具体。我的应用显示为可用于其他应用中的其他共享功能(不仅适用于 Youtube)。
这是我现在正在使用的:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
我已经查看了几个问题(大多数很像 this one)问题是这些类型的答案不准确:
<data android:host="www.youtube.com" ... />
根据data documentation 提供scheme 以使host 有效。因此,在这些答案中,只需添加主机,不会使 intent-filter 特定于 Youtube,因为没有 scheme,因此,host 将被忽略。
所以我一直在尝试通过在 Activity 启动时使用intent 的可用方法来解决这个问题:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
for (String key : bundle.keySet()) {
Log.d("KEY", key);
}
//The above loop will log
//... D/KEY: android.intent.extra.SUBJECT
//... D/KEY: android.intent.extra.TEXT
//This is are the same keys than above, but using the available constants
String subject = getIntent().getStringExtra(Intent.EXTRA_SUBJECT);
String text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
//The subject is the video title
Log.d("SUBJECT", subject);
//The text is the video url, example: https://youtu.be/p6qX_lg4wTc
Log.d("TEXT", text);
//Action is consistent with the intent-filter android.intent.action.SEND
Log.d("ACTION", intent.getAction());
//This is consistent with the intent-filter data mime type text/plain
Log.d("TYPE", intent.getType());
/*
This is the problem.
The scheme is null (that is why I'm using String value of).
*/
Log.d("scheme", String.valueOf(intent.getScheme()));
因此,当检查意图中的可用信息时,一切似乎都井然有序,但不是方案。所以,根据得到的结果,我做了一些盲目的尝试来弄清楚:
<data android:scheme="http" android:mimeType="text/plain"/>
//I'm adding youtu.be here because is the url format in the text extra
<data android:scheme="http" android:host="youtu.be" android:mimeType="text/plain"/>
添加http 或https 将不起作用,这会使应用程序不再在选择器中可用。这意味着添加host 的其他尝试都不会起作用。
有人知道如何创建一个intent-filter 专门用于 Youtube 分享吗?
PS:我知道我可以验证 url 以查看它是否与 Youtube url 匹配,但是让我的应用在每个匹配 SEND 的选择器中似乎对用户不友好
【问题讨论】: