简体   繁体   中英

Check which url is opened in custom chrome tabs

Is there any function in chrome custom tabs analogous to onPageStarted of Webview. IN onNavigation.. the bundle is always null

By design this is not possible with Chrome Custom Tabs. You can tell that a user has navigated but you can't tell where they've gone to. See: http://developer.android.com/reference/android/support/customtabs/CustomTabsCallback.html for details of what's possible.

You can see what URL is currently open in Chrome Custom Tabs if you can get the user to trigger a PendingIntent by clicking on a toolbar action button or a menu option.

In your fragment/activity, create a nested BroadcastReceiver class that will handle the incoming intent in it's onReceive() method:

class DigBroadcastReceiver() : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val uri: Uri? = intent.data
        if (uri != null) {
            Log.d("Broadcast URL",uri.toString())
            main.genericToast(uri.toString())
        }
    }
}

Add the receiver to your manifest file:

<receiver
    android:name=".ui.dig.DigTabs$DigBroadcastReceiver"
    android:enabled="true" />

Create the PendingIntent and add it to your CustomTabsIntent.Builder:

val sendLinkIntent = Intent(main,DigBroadcastReceiver()::class.java)
        sendLinkIntent.putExtra(Intent.EXTRA_SUBJECT,"This is the link you were exploring")
        val pendingIntent = PendingIntent.getBroadcast(main,0,sendLinkIntent,PendingIntent.FLAG_UPDATE_CURRENT)
        // Set the action button
        AppCompatResources.getDrawable(main, R.drawable.close_icon)?.let {
            DrawableCompat.setTint(it, Color.WHITE)
            builder.setActionButton(it.toBitmap(),"Add this link to your dig",pendingIntent,false)
        }
        val customTabsIntent: CustomTabsIntent = builder.build()
        customTabsIntent.launchUrl(main, Uri.parse(url))

See my article explaining this on Medium .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM