【问题标题】:Android 11 / API 30 - Open YouTube linkAndroid 11 / API 30 - 打开 YouTube 链接
【发布时间】:2021-04-07 10:08:28
【问题描述】:

我目前很难在浏览器或 YouTube 应用中打开 YouTube 链接。 我知道新的Package visibility in Android 11 并以这种方式实现它。

MyClass.java$myMethod

String ytLink = "https://www.youtube.com/watch?v=Hp_Eg8NMfT0"
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(ytLink)); 
if (intent.resolveActivity(context.getPackageManager()) != null) {  
  context.startActivity(intent); 
}

AndroidManifest.xml

<manifest>
  <queries>
    <intent>
      <action android:name="android.intent.action.VIEW" />
      <data android:scheme="https" />
    </intent>
  </queries> 
</manifest>

但是intent.resolveActivity 返回null... 我尝试过的所有其他链接都返回ComponentName 所以Intent 可以启动... 你有什么线索吗?

提前致谢;)

【问题讨论】:

    标签: android android-intent youtube android-11


    【解决方案1】:

    另一种选择是将主机添加到清单中的查询列表中:

    <queries>    
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data  android:scheme="https" android:host="youtube.com" />
        </intent>
    </queries>
    

    似乎仅使用方案的简单方法失败了,因为 Youtube 应用不仅过滤请求的方案,而且(出于显而易见的原因)还过滤特定主机。

    顺便说一句:当我测试时,“youtube.com”和“youtu.be”在这两种链接上都同样有效。为了安全起见,我仍然在我的应用中加入了两者。

    【讨论】:

      【解决方案2】:

      你还需要把actiondata放到Activity in Manifests的intent-filter

      <intent-filter>
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT" />
      
          <data android:scheme="https" />
      </intent-filter>
      

      您可以查看文档here

      【讨论】:

      • 嗨,如果我错了,请原谅,但是 Intent 过滤器用于声明我的应用程序的活动、服务或接收器可以处理和打开什么?我尝试做的是从我的应用程序打开一个 YouTube URL 到浏览器应用程序或 YouTube 应用程序(如果已安装),而不是在我的应用程序中打开它;)它适用于任何启动的 URL(报纸、dailymotion 等)浏览器应用程序,但对于 YouTube,没有找到可以处理它的应用程序或活动,而我们可以使用浏览器浏览到 youtube.com……也许是谷歌做出的新限制?
      • 对不起,您需要使用默认类别而不是可浏览来打开 Youtube 应用,只需将其替换为 &lt;category android:name="android.intent.category.DEFAULT" /&gt;。我和你一样在 Android 11 上启动电子邮件意图时遇到了同样的问题,我阅读了文档并遵循了它,它在新文档的零解释下运行良好。也许他们只是在没有任何有用信息的情况下更新新文档。
      • 我终于在 Intent 中添加了一个类别 CATEGORY_BROWSABLE,它正确地打开了 YouTube URL 的浏览器 :) 你关于 category 的演讲和晚安让我重新思考了它,扫描[文档][1]。 [1]:developer.android.com/training/package-visibility/…
      【解决方案3】:

      解决方案是将category CATEGORY_BROWSABLE 添加到 Intent 以允许浏览器成为处理 YouTube URL 的应用之一。 您还可以使用FLAG_ACTIVITY_REQUIRE_NON_BROWSERFLAG_ACTIVITY_REQUIRE_DEFAULT 中所述的标志documentation 来在YouTube 应用程序中打开它,但我的主要问题是没有发现任何活动,因此在浏览器中打开是一个选项;)

      【讨论】:

        猜你喜欢
        • 2015-03-10
        • 2012-02-22
        • 1970-01-01
        • 2017-05-13
        • 1970-01-01
        • 1970-01-01
        • 2022-12-19
        • 1970-01-01
        • 2012-02-27
        相关资源
        最近更新 更多