【问题标题】:Which intent-filter scheme use for Youtube in AndroidAndroid 中的 Youtube 使用哪种意图过滤器方案
【发布时间】: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"/>

添加httphttps 将不起作用,这会使应用程序不再在选择器中可用。这意味着添加host 的其他尝试都不会起作用。

有人知道如何创建一个intent-filter 专门用于 Youtube 分享吗?

PS:我知道我可以验证 url 以查看它是否与 Youtube url 匹配,但是让我的应用在每个匹配 SEND 的选择器中似乎对用户不友好

【问题讨论】:

    标签: android youtube


    【解决方案1】:

    tldr:您不能专门为 Youtube 应用创建 intent-filter,分享视频的意图没有任何限制。

    所以我对此非常着迷,找出答案的唯一方法似乎是检查 Youtube 应用程序代码。我找到了apk here,然后用this反编译。我想我在this file 中找到了代码(文本错误似乎证实了我的发现)。

    所以,如果我的推论是正确的,那么 Youtube 应用程序会创建一个 intent 而没有 scheme 或没有其他任何东西来使其具体化。它只使用SEND

    【讨论】:

      【解决方案2】:

      这不能回答您的具体问题,但我认为可以达到您想要的效果。

      您可以使用youtube android player api libraryYouTubeIntents

      像这样:

      Intent youtubeIntent = 
        YouTubeIntents.createPlayVideoIntent(getApplicationContext(), VIDEO_ID);
      startActivity(youtubeIntent);
      

      【讨论】:

      • 我希望我的应用程序从 Youtube 接收明确的意图,我不想从我的应用程序向 Youtube 发送和意图。我可以从 Youtube 接收意图,我的应用程序使用和意图过滤器注册为可用。当用户在 Youtube 上时,按下共享按钮我的应用程序在选择器中可用。我现在需要做的是让它专门用于 Youtube,因为在 coposer 中也可以用于其他可以触发 SEND 隐式意图的应用程序。为了使它仅适用于 Youtube 而不是其他人,我必须使我的意图过滤器更具体。
      猜你喜欢
      • 1970-01-01
      • 2011-04-12
      • 2015-07-14
      • 2015-05-01
      • 2015-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多