Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android launch browser without specifying a URL

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!

like image 962
MindWire Avatar asked Sep 14 '11 19:09

MindWire


Video Answer


3 Answers

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

like image 68
Diego Torres Milano Avatar answered Sep 24 '22 06:09

Diego Torres Milano


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.

like image 38
18446744073709551615 Avatar answered Sep 25 '22 06:09

18446744073709551615


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);
like image 24
user2651609 Avatar answered Sep 23 '22 06:09

user2651609