如何在android中隐藏意图选择器窗口?
当我单击该按钮时,我会启动一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请求特定的 Activity 是有风险的,因为 YouTube 可能会更改其包名称,这将导致您的应用程序崩溃。
另外 - 不保证所有 Android 设备上都安装了 YT 播放器。
为了避免这种情况,这里有一个搜索 YouTube 活动的代码。如果找到它,它会返回一个直接使用它的意图,否则,它会保留一个“通用”意图,这将导致显示系统意图选择器。
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.
您正在使用隐式意图,它可以匹配多个接收者,即选择者。如果您能弄清楚如何直接定位 YouTube 活动,您可以尝试切换到显式意图模型。请参阅开发人员文档了解显式意图与隐式意图。
然而,意图选择器的原因似乎是让每个用户自己决定使用哪个播放器。您想绕过这个有充分的理由吗?如果有人安装了他们喜欢的不同视频播放器怎么办?
编辑:要调用显式意图,您需要知道您尝试启动的活动的名称,并传递其他详细信息作为附加信息,即:
但是,我完全编造了存在 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.:
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.