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

vis*_*pta 6 youtube android hyperlink android-intent

我的问题与关于如何打开YouTube链接的其他问题类似.我的问题是如何打开YouTube链接,然后当它在应用程序中打开时,它应关闭YouTube应用程序并再次打电话给我MainActivity打开YouTube应用程序.但是,它应该通过scrath打开YouTube应用,而不仅仅是显示之前在后台运行的YouTube活动.

MainAcitivy - > 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();
Run Code Online (Sandbox Code Playgroud)

SecondActivity

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();
Run Code Online (Sandbox Code Playgroud)

ThirdActivity

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();
Run Code Online (Sandbox Code Playgroud)

我想从头开始每次加载它,但是它显示了它暂停的状态.如果您不理解我的问题,请随意发表评论,我将尝试详细说明.提前致谢.

jbr*_*ans 13

以下示例代码将在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);
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:回答你的第二个要求.每次Intent使用此代码调用new 时.它将打开此视频的应用程序或浏览器,它不会显示以前加载的视频.


Kev*_*OUX 5

Kotlin 版打开 Youtube 视频

fun openYoutubeLink(youtubeID: String) {
    val intentApp = Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + youtubeID))
    val intentBrowser = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + youtubeID))
    try {
        this.startActivity(intentApp)
    } catch (ex: ActivityNotFoundException) {
        this.startActivity(intentBrowser)
    }

}
Run Code Online (Sandbox Code Playgroud)

打电话就行

this.openYoutubeLink("Q-dNnMlaGNg")
Run Code Online (Sandbox Code Playgroud)


小智 2

以下代码将在您的手机中打开 YouTube 应用程序

Intent Intent = new Intent(Intent.ACTION_VIEW, "这里是你的 YouTube 网址"); 启动活动(意图);

如果你想在你的activity中加载url,放置一个webview并在webview中运行url