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));
A short code version...
if (!strUrl.startsWith("http://") && !strUrl.startsWith("https://")){
strUrl= "http://" + strUrl;
}
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(strUrl)));
In 2.3, I had better luck with
final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url));
activity.startActivity(intent);
The difference being the use of Intent.ACTION_VIEW
rather than the String "android.intent.action.VIEW"
Okay,I checked every answer but what app has deeplinking with same URL that user want to use?
Today I got this case and answer is browserIntent.setPackage("browser_package_name");
e.g. :
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
browserIntent.setPackage("com.android.chrome"); // Whatever browser you are using
startActivity(browserIntent);
Thank you!
Try this:
Uri uri = Uri.parse("https://www.google.com");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
or if you want then web browser open in your activity then do like this:
WebView webView = (WebView) findViewById(R.id.webView1);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webView.loadUrl(URL);
and if you want to use zoom control in your browser then you can use:
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
Just like the solutions other have written (that work fine), I would like to answer the same thing, but with a tip that I think most would prefer to use.
In case you wish the app you start to open in a new task, indepandant of your own, instead of staying on the same stack, you can use this code:
final Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY|Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(intent);
There is also a way to open the URL in Chrome Custom Tabs . Example in Kotlin :
@JvmStatic
fun openWebsite(activity: Activity, websiteUrl: String, useWebBrowserAppAsFallbackIfPossible: Boolean) {
var websiteUrl = websiteUrl
if (TextUtils.isEmpty(websiteUrl))
return
if (websiteUrl.startsWith("www"))
websiteUrl = "http://$websiteUrl"
else if (!websiteUrl.startsWith("http"))
websiteUrl = "http://www.$websiteUrl"
val finalWebsiteUrl = websiteUrl
//https://github.com/GoogleChrome/custom-tabs-client
val webviewFallback = object : CustomTabActivityHelper.CustomTabFallback {
override fun openUri(activity: Activity, uri: Uri?) {
var intent: Intent
if (useWebBrowserAppAsFallbackIfPossible) {
intent = Intent(Intent.ACTION_VIEW, Uri.parse(finalWebsiteUrl))
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
or Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
if (!CollectionUtil.isEmpty(activity.packageManager.queryIntentActivities(intent, 0))) {
activity.startActivity(intent)
return
}
}
// open our own Activity to show the URL
intent = Intent(activity, WebViewActivity::class.java)
WebViewActivity.prepareIntent(intent, finalWebsiteUrl)
activity.startActivity(intent)
}
}
val uri = Uri.parse(finalWebsiteUrl)
val intentBuilder = CustomTabsIntent.Builder()
val customTabsIntent = intentBuilder.build()
customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
or Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
CustomTabActivityHelper.openCustomTab(activity, customTabsIntent, uri, webviewFallback)
}