如何在Android应用程序中打开Youtubevideo链接?

我的问题与其他有关如何打开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(); 

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

我想每次都从头开始加载它,但是它向我显示了暂停的状态。 如果你不明白我的问题,请自由发表评论,我会尽力详细说明。 提前致谢。

下面的示例代码将打开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 。 它将打开这个video的应用程序或浏览器,它不会显示以前的video加载。

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

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

如果你想在你的活动中加载url,请在web视图中输入url并运行url

Kotlin版本打开Youtubevideo

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