如何在android中隐藏意图选择器窗口?

发布于 2024-09-02 19:59:00 字数 300 浏览 5 评论 0原文

当我单击该按钮时,我会启动一个 YouTube 视频活动,如下所示:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));

如果我使用此按钮,它会重定向到意图选择器以在浏览器或 YouTube 应用程序上打开该视频网址。如何以编程方式选择默认应用程序为 youtube?

我的输出应该直接在 YouTube 播放器上打开该视频。如何?有什么想法吗?

when i click the button i start a activity for youtube video like this:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));

if i use this its redirect to the intent chooser to open that video url on browser or youtube app. how to select the default app as youtube programmatically?

my output should open that video directly on youtube player. how? any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸢与 2024-09-09 19:59:00

请求特定的 Activity 是有风险的,因为 YouTube 可能会更改其包名称,这将导致您的应用程序崩溃。

另外 - 不保证所有 Android 设备上都安装了 YT 播放器。

为了避免这种情况,这里有一个搜索 YouTube 活动的代码。如果找到它,它会返回一个直接使用它的意图,否则,它会保留一个“通用”意图,这将导致显示系统意图选择器。

/**
 * @param context
 * @param url To display, such as http://www.youtube.com/watch?v=t_c6K1AnxAU
 * @return an Intent to start the YouTube Viewer. If it is not found, will
 *         return a generic video-play intent, and system will display a
 *         chooser to ther user.
 */
public static Intent getYouTubeIntent(Context context, String url) {
  Intent videoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
  final PackageManager pm = context.getPackageManager();
  List<ResolveInfo> activityList = pm.queryIntentActivities(videoIntent, 0);
  for (int i = 0; i < activityList.size(); i++) {
    ResolveInfo app = activityList.get(i);
    if (app.activityInfo.name.contains("youtube")) {
      videoIntent.setClassName(app.activityInfo.packageName, app.activityInfo.name);
      return videoIntent;
    }
  }
  return videoIntent;
}

Asking for a specific Activity is risky, as YouTube may change their package name which will follow with your application breaking.

Also - there is no guarantee that the YT player is installed on all Android Devices.

To circumvent, here is a code that searches for a youtube activity. If it finds it, it returns an intent to use it directly, otherwise, it keeps a "generic" intent that will result in the system intent chooser to be displayed.

/**
 * @param context
 * @param url To display, such as http://www.youtube.com/watch?v=t_c6K1AnxAU
 * @return an Intent to start the YouTube Viewer. If it is not found, will
 *         return a generic video-play intent, and system will display a
 *         chooser to ther user.
 */
public static Intent getYouTubeIntent(Context context, String url) {
  Intent videoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
  final PackageManager pm = context.getPackageManager();
  List<ResolveInfo> activityList = pm.queryIntentActivities(videoIntent, 0);
  for (int i = 0; i < activityList.size(); i++) {
    ResolveInfo app = activityList.get(i);
    if (app.activityInfo.name.contains("youtube")) {
      videoIntent.setClassName(app.activityInfo.packageName, app.activityInfo.name);
      return videoIntent;
    }
  }
  return videoIntent;
}
情丝乱 2024-09-09 19:59:00

您正在使用隐式意图,它可以匹配多个接收者,即选择者。如果您能弄清楚如何直接定位 YouTube 活动,您可以尝试切换到显式意图模型。请参阅开发人员文档了解显式意图与隐式意图。

然而,意图选择器的原因似乎是让每个用户自己决定使用哪个播放器。您想绕过这个有充分的理由吗?如果有人安装了他们喜欢的不同视频播放器怎么办?

编辑:要调用显式意图,您需要知道您尝试启动的活动的名称,并传递其他详细信息作为附加信息,即:

Intent intent = new Intent(this, YouTubeViewerActivity.class);
intent.addExtra("URI", Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM"));
startActivity(intent);

但是,我完全编造了存在 YouTubeViewerActivity 类的事实。正如我所说,通常,如果您要求某些外部服务(例如 YouTube 应用程序)执行某个操作,您将使用隐式意图模型,这样用户就可以控制使用什么应用程序。

You are using a implicit intent, which can match more than one receiver, thus the chooser. You could try switch to an explicit intent model, if you can figure out how to target the youtube activity directly. See developer documentation on explicit vs. implicit intents.

However, it seems the reason for the intent chooser is to let each user decide for themselves which player to use. Is there a good reason you want to bypass this? What if someone has installed a different video player that they prefer?

Edit: To call an explicit intent, you need to know the name of the activity you are trying to start, and you pass additional details as extras i.e.:

Intent intent = new Intent(this, YouTubeViewerActivity.class);
intent.addExtra("URI", Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM"));
startActivity(intent);

However, I totally made up the fact that there is a YouTubeViewerActivity class. As I said, typically if you are asking some outside service, like the YouTube app, to perform an action you use the implicit intent model like you have, so the user has control over what application is used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文