(Android) How to open a web browser at a specific address?

Godot Version

4.6.1

Question

I created the following code that works perfectly with geo: and tel::

func _open_url(url: String, title: String) -> void:
    var runtime = Engine.get_singleton("AndroidRuntime")
    var activity = runtime.getActivity()

    # Access the necessary classes
    var IntentClass: JavaClass = JavaClassWrapper.wrap("android.content.Intent")
    var UriClass: JavaClass = JavaClassWrapper.wrap("android.net.Uri")

    var intent = IntentClass.Intent()
    intent.setAction(IntentClass.ACTION_VIEW)
    intent.setData(UriClass.parse(url))
    activity.startActivity(IntentClass.createChooser(intent, title))

Example:

_open_url("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California", "Geo")
_open_url("tel:5551234", "Call")

But it doesn’t work with https:, nothing is displayed, not even error messages:

_open_url("https://godotengine.org/", "View Webpage")

Do I need any permissions to open a web browser?

Yes, permissions to access the Internet at the very least.

Why not just do

OS.shell_open("https://godotengine.org/")

No permissions needed to do that afaik.
And it should work for all the other things too, such as geo:

1 Like

That would make the Android permissions system pretty useless if it’s that easy to circumvent.

But if it works, it works !

Cheers !

Edit : Doh ! Tibaverus made me realize this is just a call to the default browser or html viewer.

You need internet access if your app itself needs to access the internet, but opening a url means your browser will do that, you simply ask the browser to be opened.

1 Like

No internet permission is required; it was only necessary to configure it as CATEGORY_BROWSABLE:

intent.addCategory(IntentClass.CATEGORY_BROWSABLE)

Docs: https://developer.android.com/reference/android/content/Intent#CATEGORY_BROWSABLE

Activities that can be safely invoked from a browser must support this category. For example, if the user is viewing a web page or an e-mail and clicks on a link in the text, the Intent generated to execute that link will require the BROWSABLE category, so that only activities supporting this category will be considered as possible actions. By supporting this category, you are promising that there is nothing damaging (without user intervention) that can happen by invoking any matching Intent.

1 Like

I’m not opening it within the app; as I stated in the question title, I want to open it in the browser.

As I stated in the question, I want to open the browser, and that’s all. I don’t want to open it within the application; I’m just calling the user’s browser. See my answer for the solution.

+1 This works well, although extra care is needed with the input.