Android intent to open user's preferred browse

2019-01-23 05:34发布

问题:

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?

回答1:

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).



回答2:

            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.



回答3:

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



回答4:

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?



回答5:

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);