How to find default browser set on android device
Asked Answered
S

3

26

Is there any way to find out which browser is set as a default browser on android device? On android device there may be multiple browsers installed but out of which only one set as a default. I need to find it out programmatically.

Thanks in advance. Early response is appreciated..

Sternwheeler answered 12/5, 2014 at 14:20 Comment(0)
G
38

This code may help you:

Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://"));  
ResolveInfo resolveInfo = getPackageManager().resolveActivity(browserIntent,PackageManager.MATCH_DEFAULT_ONLY);

// This is the default browser's packageName
String packageName = resolveInfo.activityInfo.packageName;

and if wanna start it, do as follows:

startActivity(getPackageManager().getLaunchIntentForPackage(packageName));
Georginegeorglana answered 13/5, 2014 at 3:32 Comment(6)
If you juste want the name of the app (like "Chrome"), use this : resolveInfo.loadLabel(getPackageManager()).toString();Acumen
this seems to be totally wrong based on https://mcmap.net/q/180511/-what-is-the-purpose-of-quot-android-intent-category-default-quotSwetlana
@Georginegeorglana do you know how to go direct to the setting of that applications.Haslet
For me (Android 9) this consistently returned android (the picker, I think), even when a default browser was properly selected. Changing the URI to a real URI (http://example.com) fixed it.Aciculate
@TimPerry Your solution also returned 'android' for android 7. any idea? please let me know.Instrumentality
If your app targets Android 11 or higher, you must add a declaration in the <queries> element in the manifest for this to work, as described here developer.android.com/training/package-visibility/use-casesQuerist
K
5

You are welcome to use PackageManager and resolveActivity() to attempt to determine what activity (in what app) will handle a particular Intent. However, this may indicate that the chooser will handle the request, because there is no current default (e.g., user just installed a new browser, and so the chooser will appear for the next Web browser request).

Kuntz answered 12/5, 2014 at 14:23 Comment(0)
E
0

The accepted answer works well however, I thought it would be good to show you how I implemented the functionality in Kotlin and took care of different SDKs. The gist is available at: https://gist.github.com/Hesamedin/3e0766a8ee743488e61c2386af6d6f69

The final result would be like this:

BrowserInformation: { AppName: Samsung Internet, Version: 1230101502, PackageName: com.sec.android.app.sbrowser }

You can use this extension function in your Activity/Fragment.

private fun logBrowserInfo() {
    val pair = this.getDefaultBrowserInfo()
    val info = pair.first
    val error = pair.second
}

Context Extension

fun Context?.getDefaultBrowserInfo(): Pair<String, Exception?> {
    val ctx = this ?: return Pair("", null)
    var browserInfo = ""
    var error: Exception? = null
    try {
        val packageManager = ctx.packageManager
        browserInfo = packageManager.defaultBrowserInfo()
    } catch (e: PackageManager.NameNotFoundException) {
        e.printStackTrace()
        error = e
    }
    return Pair(browserInfo, error)
}

private fun PackageManager.defaultBrowserInfo(): String {
    val browserIntent = Intent("android.intent.action.VIEW", Uri.parse("http://"))
    var appName = ""
    var appVersion = ""
    var packageName = ""
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        val resolveInfo = resolveActivity(
            browserIntent,
            PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong())
        )
        packageName += resolveInfo?.activityInfo?.packageName
        appName += resolveInfo?.loadLabel(this)
        val packageInfo = this.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0))
        appVersion += getPackageVersion(packageInfo)
    } else {
        val resolveInfo = @Suppress("DEPRECATION") resolveActivity(
            browserIntent,
            PackageManager.MATCH_DEFAULT_ONLY
        )
        packageName += resolveInfo?.activityInfo?.packageName
        val packageInfo = @Suppress("DEPRECATION") this.getPackageInfo(packageName, 0)
        appVersion += getPackageVersion(packageInfo)
    }

    val sb = StringBuilder()
    sb.append("BrowserInformation: { ")
    sb.append("AppName: ").append(appName).append(", ")
    sb.append("Version: ").append(appVersion).append(", ")
    sb.append("PackageName: ").append(packageName)
    sb.append(" }")
    return sb.toString()
}

private fun getPackageVersion(packageInfo: PackageInfo): String {
    try {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            packageInfo.longVersionCode.toString()
        } else {
            @Suppress("DEPRECATION") packageInfo.versionCode.toString()
        }
    } catch (e: PackageManager.NameNotFoundException) {
        e.printStackTrace()
    }
    return "N/A"
}
Enravish answered 3/1, 2024 at 16:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.