How can I open a URL in Android's web browser from my application?
Try this:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
That works fine for me.
As for the missing "http://" I'd just do something like this:
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
I would also probably pre-populate your EditText that the user is typing a URL in with "http://".
Android App link - Open a url from app in browser without triggering App Link
String data = "example.com/your_url?param=some_param";
Intent defaultBrowser = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_BROWSER);
defaultBrowser.setData(data);
startActivity(defaultBrowser);
this technique (using makeMainSelectorActivity) will force the link to open in the device's default browser
Note - makeMainSelectorActivity only works for API level 15 and above.
If you need to support API levels lower than 15, you could try this hack
Is there any way in Android to force open a link to an external browser?
Your implementation looks good to me and you can use it whether chrome is installed or not but you need to check if it is resolvable or not:
if (url != null) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
If you want to show list of browsers to user to choose from you can use:
Intent sendIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
Intent chooser = Intent.createChooser(sendIntent, "Choose Your Browser");
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
If your targetSdk is 30 or above take a look at this document too:
https://developer.android.com/training/package-visibility/use-cases
Sending an Intent to browser to open specific URL
To open a URL/website you do the following:
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Here's the documentation of Intent.ACTION_VIEW
.
Source: Opening a URL in Android's web browser from within application
Android, open url by my application
I have solved my problem by using these methods.
In my web page, I have added link
tags to head
:
<link rel="alternate" href="android-app://com.example.app/example.com/my/path/to/information"/>
In my manifest, I have added this:
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:host="example.com" android:pathPattern="/.*" android:scheme="http"/>
<data android:host="example.com" android:pathPattern="/.*" android:scheme="https"/>
<data android:host="example.com" android:pathPattern="/.*" android:scheme="android-app"/>
<data android:scheme="http"/>
</intent-filter>
This worked. Now when I click to links it suggests my application to open it.
Related Topics
How to Make My Custom Objects Parcelable
How to Make Links in a Textview Clickable
How to Format Date and Time in Android
Communicating Between a Fragment and an Activity - Best Practices
Passing a Bundle on Startactivity()
How to Display a Toast from a Background Thread on Android
How Can an App Detect That It's Going to Be Uninstalled
Android Completely Transparent Status Bar
How to Get Complete Address from Latitude and Longitude
Scheduling Recurring Task in Android
How to Send One to One Message Using Firebase Messaging
How to Connect to My Http://Localhost Web Server from Android Emulator
How to Check a Certain Data Already Exists in Firestore or Not
Android 11 Scoped Storage Permissions
Android Studio Adds Unwanted Permission After Running Application on Real Device