如何在 Android 应用程序中打开 Youtube 视频链接?

2023-11-25

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

MainActivity --> SecondActivity --> 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);
    }
}

编辑:回答你的第二个要求。每次你调用一个新的Intent用这个代码。它将打开该视频的应用程序或浏览器,并且不会显示之前加载的视频。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Android 应用程序中打开 Youtube 视频链接? 的相关文章

随机推荐