如何在Android应用中打开YouTube视频链接? [英] How to open Youtube video link in android app?

查看:60
本文介绍了如何在Android应用中打开YouTube视频链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与其他有关如何打开YouTube链接的问题不同。我的问题是如何打开YouTube链接,然后当它在应用程序中打开时,它应该关闭YouTube应用程序,然后再次调用我的MainActivity,这将打开YouTube应用程序。不过,它应该从scrath打开YouTube应用程序,而不是只显示在后台运行的前一个YouTube活动。

MainAcitivy-->Second Activity-->YouTube-->ThirdActivity-->YouTube

但我希望重新从头开始加载YouTube应用程序。但目前,我正在访问之前打开的YouTube应用程序,该应用程序位于后台。

维护活动

Intent intent = new Intent(MainActivity.this,ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

辅助活动

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(link)));
sleep(10000);
Intent intent=new Intent(getApplicationContext(),ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

第三个活动

sleep(5000);
Toast.makeText(getApplicationContext(),"third",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
我希望每次从头开始再次加载它,但它显示了暂停的状态。如果您不明白我的问题,请您自由发表意见,我会尽量详细说明。提前谢谢。

推荐答案

以下示例代码将在youtube应用程序中打开youtube链接(如果此链接可用),否则将在浏览器中打开它:

public static void watchYoutubeVideo(String id) {
    Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id));
    Intent webIntent = new Intent(Intent.ACTION_VIEW,
    Uri.parse("http://www.youtube.com/watch?v=" + id));
    try {
        startActivity(appIntent);
    } catch (ActivityNotFoundException ex) {
        startActivity(webIntent);
    }
}

edit:回答您的第二个需求。每次使用此代码调用新的Intent时。它将打开此视频的应用程序或浏览器,并且不会显示加载的前一个视频。

这篇关于如何在Android应用中打开YouTube视频链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆