简体   繁体   中英

Android intent to open user's preferred browser

I've been trying to find out how to create an intent that will open the user's preferred browser without specifying the URL. I know how to open it by giving a specific URL like this:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(android.net.Uri.parse("http://www.google.com"));
context.startActivity(intent);

I don't want to open the browser to any page in particular, just the set homepage or whatever page the user was on last. I've thought about looking up the homepage set within the app but you can't do it with the default Browser app because it is private. Does anybody know of a way to do this?

Here's how I did it:

String packageName = "com.android.browser"; 
String className = "com.android.browser.BrowserActivity"; 
Intent internetIntent = new Intent(Intent.ACTION_VIEW);
internetIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
internetIntent.setClassName(packageName, className); 
mHomeActivity.startActivity(internetIntent);

If you have no homepage set, it'll open a blank page (at least in Android 2.1).

            Uri uri = Uri.parse("www.google.com");
            Intent intent = new Intent(Intent.ACTION_VIEW,uri);
            // Create and start the chooser
            Intent chooser = Intent.createChooser(intent, "Open with");
            startActivity(chooser);

This code creates an intent to open user specified browser.

This is a late answer, but looks like this functionality is available in API 15:

  Intent browser = Intent.makeMainSelectorActivity(
         Intent.ACTION_VIEW, 
         Intent.CATEGORY_APP_BROWSER);

  startActivity(browser);

Docs for makeMainSelectorActivity

The homepage URL specified by the user will be stored in the preferences for whichever browser app they're using. With Androids 'sandbox' model for apps, you'll have no access to this unless the app has a Content Provider which allows access. In addition, the content provider will differ between the browser apps and you'll have a hard time covering the ones that do exist.

Have you tried opening to a web page which attempts to update the users homepage URL through the use of JavaScript?

Try this:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_BROWSER);
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent.createChooser(intent, "Select Browser"));
}
else { //Popup a msg so as to know the reason of not opening the browser
    Toast.makeText(this, "There is no Browser App", Toast.LENGTH_LONG).show();
}

Worked for me, hope for you also!

Use below code

Intent sendIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com"));
Intent chooser = Intent.createChooser(sendIntent, "Choose Your Browser");
if (sendIntent.resolveActivity(getPackageManager()) != null) 
    startActivity(chooser);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM