我已经构建了一个使用Chrome自定义标签的Android应用可以选择的浏览器应用程序。我的问题是如何确定该应用仅将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))
}
我找到了如何验证它,引用了此问题,因此我对此进行了编码和工作,不再要求设置浏览器。
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)
}
以下代码来自此处,但请记住,我们应该尊重用户的喜好。根据用户偏好,我的意思是
- 如果用户有多个支持自定义选项卡的浏览器,则不具有默认浏览器,其中一个浏览器是Chrome,我们可以打开Chrome的链接。
-
如果用户有多个支持的浏览器自定义选项卡,并具有一个默认浏览器A设置,然后浏览器A具有自定义标签支持,我们应该在该浏览器a。
中打开链接 如果用户有多个支持自定义选项卡但不支持的浏览器有默认浏览器,其中一个浏览器不是chrome,那么我们可以使用支持的自定义标签浏览器显示Chooser对话框以打开链接
也没有其他用例,但希望您能得到此
的关键浏览器支持CCT(至少具有最新版本)
- 三星浏览器
- Firefox
- Microsoft Edge
无CCT支持的浏览器:
旧浏览器在旧的Android版本中构建,Samsung Android设备
opera
duckduckgo
public static String getPackageNameToUse(Context context) { String packageNameToUse = null; final PackageManager packageManager = context.getPackageManager(); final Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); // Get all apps that can handle VIEW intents and have custom tab service final List<ResolveInfo> resolvedActivityList = packageManager.queryIntentActivities(activityIntent, 0); final List<String> packagesSupportingCustomTabs = new ArrayList<>(); for (final ResolveInfo info : resolvedActivityList) { final Intent serviceIntent = new Intent(); serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION); serviceIntent.setPackage(info.activityInfo.packageName); if (packageManager.resolveService(serviceIntent, 0) != null) packagesSupportingCustomTabs.add(info.activityInfo.packageName); } // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents // and service calls. if (packagesSupportingCustomTabs.size() == 1) packageNameToUse = packagesSupportingCustomTabs.get(0); else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) packageNameToUse = STABLE_PACKAGE; return packageNameToUse; }