Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 262
developer_7 Avatar asked Jun 28 '10 15:06

developer_7


1 Answers

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!

like image 196
Stavros Avatar answered Oct 08 '22 14:10

Stavros