简体   繁体   English

检查在自定义 chrome 选项卡中打开了哪个 url

[英]Check which url is opened in custom chrome tabs

Is there any function in chrome custom tabs analogous to onPageStarted of Webview. chrome 自定义选项卡中是否有类似于 Webview 的 onPageStarted 的任何功能。 IN onNavigation.. the bundle is always null在 onNavigation 中 .. 捆绑包始终为空

By design this is not possible with Chrome Custom Tabs.根据设计,Chrome 自定义标签无法做到这一点。 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.有关可能的详细信息,请参阅: http : //developer.android.com/reference/android/support/customtabs/CustomTabsCallback.html

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.如果您可以通过单击工具栏操作按钮或菜单选项让用户触发 PendingIntent,则您可以查看 Chrome 自定义标签中当前打开的 URL。

In your fragment/activity, create a nested BroadcastReceiver class that will handle the incoming intent in it's onReceive() method:在您的片段/活动中,创建一个嵌套的 BroadcastReceiver 类,该类将在其 onReceive() 方法中处理传入的意图:

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:创建 PendingIntent 并将其添加到您的 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 .请参阅我在 Medium 上对此进行解释的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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