Pra*_*een 4 video android android-intent
当我点击按钮时,我为youtube视频开始一个活动,如下所示:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));
Run Code Online (Sandbox Code Playgroud)
如果我使用它的重定向到意图选择器在浏览器或youtube应用程序上打开该视频网址.如何以编程方式选择默认应用作为youtube?
我的输出应该直接在youtube播放器上打开该视频.怎么样?任何的想法?
要求特定活动是有风险的,因为YouTube可能会更改其包裹名称,随后您的申请将被破坏.
此外 - 无法保证所有Android设备上都安装了YT播放器.
为规避,这是一个搜索youtube活动的代码.如果找到它,则返回直接使用它的意图,否则,它保持"通用"意图,这将导致系统意图选择器被显示.
/**
* @param context
* @param url To display, such as http://www.youtube.com/watch?v=t_c6K1AnxAU
* @return an Intent to start the YouTube Viewer. If it is not found, will
* return a generic video-play intent, and system will display a
* chooser to ther user.
*/
public static Intent getYouTubeIntent(Context context, String url) {
Intent videoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
final PackageManager pm = context.getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(videoIntent, 0);
for (int i = 0; i < activityList.size(); i++) {
ResolveInfo app = activityList.get(i);
if (app.activityInfo.name.contains("youtube")) {
videoIntent.setClassName(app.activityInfo.packageName, app.activityInfo.name);
return videoIntent;
}
}
return videoIntent;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3846 次 |
| 最近记录: |