我开发了自己的媒体播放器,我希望当我想从 Youtube 观看视频时,它应该从我的应用程序中打开。至少, View 上应该有一个媒体应用程序列表,我应该能够从列表中选择我的应用程序。我对 Intent 过滤器了解不多。我如何在我的 list 文件中提供这个?我正在努力为本地文件进行训练,但我也还没能做到。我真的需要你的帮助:/
最佳答案
如果您查看官方 youtube 应用程序使用的 intent 过滤器,您会看到:
<activity android:name="Player">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="youtube.com" android:pathPrefix="/watch" />
<data android:scheme="https" android:host="youtube.com" android:pathPrefix="/watch" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="*.youtube.com" android:pathPrefix="/watch" />
<data android:scheme="https" android:host="*.youtube.com" android:pathPrefix="/watch" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/v/" />
<data android:scheme="https" android:host="www.youtube.com" android:pathPrefix="/v/" />
<data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/e/" />
<data android:scheme="https" android:host="www.youtube.com" android:pathPrefix="/e/" />
<data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/embed/" />
<data android:scheme="https" android:host="www.youtube.com" android:pathPrefix="/embed/" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="vnd.youtube" />
</intent-filter>
</activity>
要获取视频 ID,您可以解析从中获取的字符串:
Uri uri = this.getIntent().getData();
如您所见,youtu.be 没有过滤器,但可以添加:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="youtu.be" />
<data android:scheme="https" android:host="youtu.be" />
</intent-filter>
关于java - 媒体播放器的 Intent 过滤器(Youtube 视频播放),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4487867/