Android 启动浏览器而不指定 URL
如何在不指定 url 的情况下从 Activity 启动浏览器。我想打开浏览器,以便用户可以继续浏览而不更改他们所在的页面?
解决方案: 下面的答案是正确的并且有效,但为了对未来的读者更具体,这里是工作代码:
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setAction("com.android.browser");
ComponentName comp = new ComponentName("com.android.browser", "com.android.browser.BrowserActivity");
i.setComponent(comp);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
谢谢!
How would one go about launching the browser from an activity without specifying a url. I would like to open the browser so the user can continue browsing without changing the page they were on?
SOLUTION:
Answer below was correct and worked, but to be more specific for future readers, here is the working code:
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setAction("com.android.browser");
ComponentName comp = new ComponentName("com.android.browser", "com.android.browser.BrowserActivity");
i.setComponent(comp);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 意图#setComponent() 设置浏览器的包名和类名。然后开始活动。
Use Intent#setComponent() to set the Browser's package and class name. Then start the activity.
如果将来它(ComponentName("com.android.browser", "com.android.browser.BrowserActivity"))发生变化,您可以尝试类似以下代码:
基本上,这里我构建了一个意图来查看虚拟 http 页面,获取可以处理该意图的活动列表,将其与resolveActivity() 返回的默认处理程序进行比较并返回一些内容。我不需要检查是否有启动器 MAIN 操作(我的代码使用 VIEW 操作),但您可能应该这样做。
In case it (the ComponentName("com.android.browser", "com.android.browser.BrowserActivity")) changes in the future, you may try something like the following code:
Basically, here I construct an intent to view a dummy http page, get the list of activities that may handle the intent, compare it to the default handler returned by resolveActivity() and return something. I do not need to check if there's a launcher MAIN action (my code uses the VIEW action), but you probably should.
这个答案可能有帮助。来自 如何打开默认android浏览器不指定URL?
this answer may help. from How to open the default android browser without specifying an URL?