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

时间:2017-02-03 12:13:16

标签: android android-intent hyperlink youtube

我的问题与其他有关如何打开YouTube链接的问题类似。我的问题是如何打开YouTube链接,然后当它在应用中打开时,它应该关闭YouTube应用并再次拨打我的MainActivity来打开YouTube应用。但是,它应该从诈烂中打开YouTube应用,而不仅仅是显示之前在后台运行的YouTube活动。

MainAcitivy - > SecondActivity - > Youtube - > ThirdActivity - >的Youtube

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

MainActivity

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

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();

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();

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

3 个答案:

答案 0 :(得分:12)

以下示例代码将在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时。它将打开此视频的应用程序或浏览器,它不会显示以前加载的视频。

答案 1 :(得分:3)

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)
    }

}

致电

this.openYoutubeLink("Q-dNnMlaGNg")

答案 2 :(得分:1)

以下代码将打开手机中的youtube应用

Intent intent = new Intent(Intent.ACTION_VIEW,“你的youtube url here”);                         startActivity(意向);

如果您想在活动中加载网址,请输入网页视图并在网页视图中运行网址