When developing Android applications that open URLs, it is essential to check if a web browser that supports the ACTION_VIEW intent is available on the device. Failing to do so can result in an ActivityNotFoundException or a NullPointerException that may crash your application. This is especially true if the targeted device is under Mobile Device Management (MDM) control, as the MDM administrator may not have installed a browser on the device.

In this article, we’ll explore the potential problems that may occur when opening a URL on Android and why you should always check if there’s a browser available on the device.

The simplified code to open a URL on Android looks like this:

val intent = Intent(Intent.ACTION_VIEW, Uri.parse(myWebUrl))
context.startActivity(intent)

This code initializes an implicit intent to launch a web browser to view a webpage specified by myWebUrl. The code itself appears to be valid, but there are several factors that could cause the code to crash.

The first problem that may occur is if myWebUrl is null or empty. Calling Uri.parse(myWebUrl) with an empty or null value will result in a NullPointerException. Therefore, you should ensure that myWebUrl is properly initialized before calling this code.

The second issue is if context is null or invalid. Calling context.startActivity(intent) with a null or invalid context will result in a NullPointerException or IllegalStateException. You should ensure that context is a valid context before calling this code.

The third problem that may arise is if there is no web browser installed on the device. If there is no web browser installed on the device, calling startActivity() with an ACTION_VIEW intent will result in an ActivityNotFoundException. To avoid this exception, you should check if a web browser is available on the device using the PackageManager.resolveActivity() method before calling startActivity().

Here’s an example of how to check if a web browser is available before opening a URL on Android:

val intent = Intent(Intent.ACTION_VIEW, Uri.parse(myWebUrl))

// Check if a web browser that supports the intent is available
val packageManager = context.packageManager
if (intent.resolveActivity(packageManager) != null) {
    context.startActivity(intent)
} else {
    // No web browser available
    Toast.makeText(context, "No web browser found", Toast.LENGTH_SHORT).show()
}

In this example, we first check if a web browser that supports the ACTION_VIEW intent is available on the device. If a browser is found, the code launches the browser to open the specified URL. If no browser is available, the code displays a Toast message indicating that no browser is found.

It is important to note, that due to the new restrictions on “package visibility” introduced in Android 11, this only works, if you allowed the app to query other apps. You can do so by adding the following to your AndroidManifest.xml:

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" />
    </intent>
</queries>

As mentioned earlier, if the targeted device is under MDM control, the MDM administrator may not have installed a browser on the device. Therefore, it is essential to check if a browser is available before launching the intent to open a URL. This will ensure that your application does not crash, and your users have a better experience.

In conclusion, when opening a URL on Android, it is essential to check if a web browser that supports the ACTION_VIEW intent is available on the device. Failing to do so can result in your application crashing or displaying an error message to your users. By checking if a browser is available before launching the intent, you can ensure that your application is more robust and provides a better user experience.