怎么在Android应用中打开YouTube视频链接?
本教程将介绍如何在Android应用中打开YouTube视频链接?的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。
问题描述
我的问题与其他有关怎么打开YouTube链接的问题不同。我的问题是怎么打开YouTube链接,然后当它在应用程序中打开时,它应该关闭YouTube应用程序,然后再次调用我的MainActivity
,这将打开YouTube应用程序。不过,它应该从scrath打开YouTube应用程序,而不是只显示在后台运行的前一个YouTube活动。
MainAcitivy-->Second Activity-->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);
}
}
edit:回答您的第二个需求。每次使用此代码调用新的Intent
时。它将打开此视频的应用程序或浏览器,并且不会显示加载的前一个视频。
好了关于怎么在Android应用中打开YouTube视频链接?的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。