How to open an URL from code in the built-in web browser rather than within my application?
I tried this:
try {
Intent myIntent = new Intent(Int
You can see the official sample from Android Developer.
/**
* Open a web page of a specified URL
*
* @param url URL to open
*/
public void openWebPage(String url) {
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Please have a look at the constructor of Intent:
public Intent (String action, Uri uri)
You can pass android.net.Uri
instance to the 2nd parameter, and a new Intent is created based on the given data url.
And then, simply call startActivity(Intent intent)
to start a new Activity, which is bundled with the Intent with the given URL.
if
check statement?Yes. The docs says:
If there are no apps on the device that can receive the implicit intent, your app will crash when it calls startActivity(). To first verify that an app exists to receive the intent, call resolveActivity() on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call startActivity(). If the result is null, you should not use the intent and, if possible, you should disable the feature that invokes the intent.
You can write in one line when creating the Intent instance like below:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));