Android 启动浏览器而不指定 URL

发布于 2024-12-04 14:48:25 字数 495 浏览 1 评论 0原文

如何在不指定 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 技术交流群。

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

发布评论

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

评论(3

紅太極 2024-12-11 14:48:25

使用 意图#setComponent() 设置浏览器的包名和类名。然后开始活动。

Use Intent#setComponent() to set the Browser's package and class name. Then start the activity.

记忆で 2024-12-11 14:48:25

如果将来它(ComponentName("com.android.browser", "com.android.browser.BrowserActivity"))发生变化,您可以尝试类似以下代码:

public static ComponentName getDefaultBrowserComponent(Context context) {
    Intent i = new Intent()
        .setAction(Intent.ACTION_VIEW)
        .setData(new Uri.Builder()
                .scheme("http")
                .authority("x.y.z")
                .appendQueryParameter("q", "x")
                .build()
                );
    PackageManager pm = context.getPackageManager();
    ResolveInfo default_ri = pm.resolveActivity(i, 0); // may be a chooser
    ResolveInfo browser_ri = null;
    List<ResolveInfo> rList = pm.queryIntentActivities(i, 0);
    for (ResolveInfo ri : rList) {
        if (ri.activityInfo.packageName.equals(default_ri.activityInfo.packageName)
         && ri.activityInfo.name.equals(default_ri.activityInfo.name)
        ) {
            return ri2cn(default_ri);
        } else if ("com.android.browser".equals(ri.activityInfo.packageName)) {
            browser_ri = ri;
        }
    }
    if (browser_ri != null) {
        return ri2cn(browser_ri);
    } else if (rList.size() > 0) {
        return ri2cn(rList.get(0));
    } else if (default_ri == null) {
        return null;
    } else {
        return ri2cn(default_ri);
    }
}
private static ComponentName ri2cn(ResolveInfo ri) {
    return new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name);
}

基本上,这里我构建了一个意图来查看虚拟 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:

public static ComponentName getDefaultBrowserComponent(Context context) {
    Intent i = new Intent()
        .setAction(Intent.ACTION_VIEW)
        .setData(new Uri.Builder()
                .scheme("http")
                .authority("x.y.z")
                .appendQueryParameter("q", "x")
                .build()
                );
    PackageManager pm = context.getPackageManager();
    ResolveInfo default_ri = pm.resolveActivity(i, 0); // may be a chooser
    ResolveInfo browser_ri = null;
    List<ResolveInfo> rList = pm.queryIntentActivities(i, 0);
    for (ResolveInfo ri : rList) {
        if (ri.activityInfo.packageName.equals(default_ri.activityInfo.packageName)
         && ri.activityInfo.name.equals(default_ri.activityInfo.name)
        ) {
            return ri2cn(default_ri);
        } else if ("com.android.browser".equals(ri.activityInfo.packageName)) {
            browser_ri = ri;
        }
    }
    if (browser_ri != null) {
        return ri2cn(browser_ri);
    } else if (rList.size() > 0) {
        return ri2cn(rList.get(0));
    } else if (default_ri == null) {
        return null;
    } else {
        return ri2cn(default_ri);
    }
}
private static ComponentName ri2cn(ResolveInfo ri) {
    return new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name);
}

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.

皇甫轩 2024-12-11 14:48:25

这个答案可能有帮助。来自 如何打开默认android浏览器不指定URL?

PackageManager pm = getPackageManager();
Intent queryIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
ActivityInfo af = queryIntent.resolveActivityInfo(pm, 0);
Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.setClassName(af.packageName, af.name);
startActivity(launchIntent);

this answer may help. from How to open the default android browser without specifying an URL?

PackageManager pm = getPackageManager();
Intent queryIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
ActivityInfo af = queryIntent.resolveActivityInfo(pm, 0);
Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.setClassName(af.packageName, af.name);
startActivity(launchIntent);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文