Shi*_*hin 1 android android-webview kotlin chrome-custom-tabs android-customtabs
我已经使用 Chrome 自定义选项卡构建了一个 Android 应用程序,但是当我单击按钮显示 URL 时,会出现一个对话框,提示我需要选择“打开方式”并列出所有可供选择的浏览器应用程序。我的问题是如何确定应用程序仅将 Chrome 设置为默认值,而不必再打开“打开方式”对话框。
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))
}
Run Code Online (Sandbox Code Playgroud)
我找到了如何验证它,参考了这个,所以我得到了这个编码并工作,不再要求设置浏览器。
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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2864 次 |
| 最近记录: |