【问题标题】:How to open Youtube video link in android app?如何在安卓应用中打开 Youtube 视频链接?
【发布时间】:2017-06-20 19:31:44
【问题描述】:

我的问题与其他有关如何打开 YouTube 链接的问题不同。我的问题是如何打开 YouTube 链接,然后当它在应用程序中打开时,它应该关闭 YouTube 应用程序并再次调用我的 MainActivity,它会打开 YouTube 应用程序。但是,它应该从头开始打开 ​​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();

第二个活动

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 应用程序的状态

标签: android android-intent hyperlink youtube


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      Intent intent = new Intent(Intent.ACTION_VIEW, "你的 youtube 网址在这里"); 开始活动(意图);

      如果您想在活动中加载 url,请放置一个 webview 并在 webview 中运行 url

      【讨论】:

      • 网址应该是这样的 --> Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("youtube_url"));
      猜你喜欢
      • 2017-05-13
      • 1970-01-01
      • 2019-01-16
      • 2015-10-14
      • 2015-03-10
      • 2014-10-13
      • 1970-01-01
      • 2014-10-18
      相关资源
      最近更新 更多