【问题标题】:how to open youtube app from android application如何从 android 应用程序打开 youtube 应用程序
【发布时间】:2013-10-07 15:20:41
【问题描述】:

我有 youtube 按钮,它将打开一个 特定 *频道* 我希望它在 youtube 中打开strong> *application* 以便从我的应用程序访问频道。

Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("https://www.youtube.com/channel/UCRmoG8dTnv0B7y9uoocikLw"));
context.startActivity(intent);

但它正在浏览器中打开。

【问题讨论】:

  • 试试这个 startActivity(new Intent("android.intent.action.VIEW", Uri.parse("url")));
  • @Rohit 与 OP 的代码完全相同吗?
  • 试试这个android.intent.action.VIEW

标签: android android-intent


【解决方案1】:

您应该明确地将其发送到 youtube。您可以通过指定包来做到这一点:

intent.setComponent(new ComponentName("com.google.android.youtube","com.google.android.youtube.PlayerActivity"));

另外请注意,您还应该检查是否安装了 youtube!

Intent intent = new Intent(
    Intent.ACTION_VIEW , 
    Uri.parse("https://www.youtube.com/channel/UCRmoG8dTnv0B7y9uoocikLw"));
intent.setComponent(new ComponentName("com.google.android.youtube","com.google.android.youtube.PlayerActivity"));

PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
if (infos.size() > 0) {
     context.startActivity(intent);
}else{
     //No Application can handle your intent
}

【讨论】:

  • 另请注意,您可能希望在 startActivity 调用周围捕获 ActivityNotFoundException 以回退到浏览器 youtube 播放器。
  • 正确@elimirks,我已将其添加到我的答案中。
  • 我认为这不是正确的方法,主要是因为应用程序提供商可以更新/更改包名称/活动名称。所有应用程序都应该由正确的意图操作启动。虽然,这可能不会导致任何直接问题,但很容易破坏。 (取决于谷歌...)
  • 是的,但另一种解决方案是将 Youtube 设置为此类 URL 的主要处理程序。
【解决方案2】:

我发现这是一种简单的方法(假设您已经检查过 YouTube 已安装):

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.youtube.com/**YOURCHANNEL**"));
intent.setPackage("com.google.android.youtube");
startActivity(intent);

【讨论】:

  • 感谢YouTube的包名
【解决方案3】:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlStr));
startActivity(intent);

urlStr 是您想要的频道的网址

【讨论】:

    【解决方案4】:
    Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://www.youtube.com/"));
                intent.setPackage("com.google.android.youtube");
                PackageManager manager = getPackageManager();
                List<ResolveInfo> info = manager.queryIntentActivities(intent, 0);
                if (info.size() > 0) {
                    startActivity(intent);
                } else {
                    //No Application can handle your intent
                }
    

    这就是您可以实现的方式.. 仅提供“https://www.youtube.com/”将直接将您带入主页。虽然绝对可以自定义 url 以重定向到特定的频道或视频。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-29
      • 2014-06-20
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      相关资源
      最近更新 更多