Search code examples
androidkotlinandroid-webviewchrome-custom-tabsandroid-customtabs

How to set default Chrome custom tabs didn't have to show “open with”


I have built an Android app with Chrome Custom Tabs, but when I clicked the button to show the URL, a dialog appear say I need to choose "Open With" and list of all my browser apps that are available to be chosen. My problem is how to determine that the app only set Chrome as default and don't have to open dialog "open with" anymore.

fab.setOnClickListener {
    val url = "http://myurl.com/"
    val builder = CustomTabsIntent.Builder()
    builder.setToolbarColor(ContextCompat.getColor(this@MainActivity,R.color.colorAccent))
    builder.addDefaultShareMenuItem()

    val anotherCustomTab = CustomTabsIntent.Builder().build()

    val intent = anotherCustomTab.intent
    intent.data = Uri.parse("http://myurl.com/")

    builder.setShowTitle(true)

    val customTabsIntent = builder.build()
    customTabsIntent.launchUrl(this@MainActivity, Uri.parse(url))
}

Solution

  • I found how to verify it, referenced to this so i got this coded and work, didnt ask to set browser anymore.

    fab.setOnClickListener {
    
            val PACKAGE_NAME = "com.android.chrome"
    
            val builder = CustomTabsIntent.Builder()
    
            builder.setToolbarColor(ContextCompat.getColor(this@MainActivity,R.color.colorAccent))
            builder.addDefaultShareMenuItem()
            builder.setShowTitle(true)
    
            val anotherCustomTab = builder.build()
            val intent = anotherCustomTab.intent
            intent.data = Uri.parse("http://www.myurl.com/")
    
            val packageManager = packageManager
            val resolveInfoList = packageManager.queryIntentActivities(anotherCustomTab.intent, PackageManager.MATCH_DEFAULT_ONLY)
    
            for (resolveInfo in resolveInfoList) {
                val packageName = resolveInfo.activityInfo.packageName
                if (TextUtils.equals(packageName, PACKAGE_NAME))
                    anotherCustomTab.intent.setPackage(PACKAGE_NAME)
            }
            anotherCustomTab.launchUrl(this, anotherCustomTab.intent.data)
        }