Android 11/API 30-打开YouTube链接



我目前很难在浏览器或YouTube应用程序中打开YouTube链接。我知道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就可以启动了。。。你有线索吗?

提前感谢;(

还有一个选项是将主机添加到清单中的查询列表中:

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

似乎只有该方案的简单方法失败了,因为Youtube应用程序不仅过滤请求的方案,而且(出于明显的原因(过滤特定主机。

BTW:当我测试时;youtube.com";以及";youtu.be;在这两种链接上都做了同样的工作。为了安全起见,我还是把这两个都放在了我的应用程序中。

您还需要在Manifests 中放入活动的actiondataintent-filter

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

你可以在这里查看文档

解决方案是在Intent中添加一个categoryCATEGORY_BROWSABLE,以允许浏览器成为处理YouTube URL的应用程序之一。你也可以使用文档中描述的标志FLAG_ACTIVITY_REQUIRE_NON_BROWSERFLAG_ACTIVITY_REQUIRE_DEFAULT在YouTube应用程序中打开它,但我的主要问题是没有发现任何活动,所以可以选择在浏览器中打开;(

最新更新