我正试图在我的应用程序中添加CustomTabIntent以打开我的网站的网址。问题是我已经将AppLinking实现到我的应用程序中,因为它没有出现chrome,所以它被重定向到我的deeplink处理程序类,它将我的url重定向到Chrome。
我为CustomTabIntent使用了以下代码,
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder()
.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary))
.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark))
.setShowTitle(true)
.addDefaultShareMenuItem()
.setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left)
.setExitAnimations(this, android.R.anim.slide_in_left,
android.R.anim.slide_out_right);
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));它们是我可以绕过applink打开CustomTabIntent的一种方式吗?
发布于 2018-06-07 07:21:59
是的,您可以通过访问CustomTabsIntent中的内部意图并调用setPackageName来设置要打开的应用程序的包名。
String packageName = "...";
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder()
.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary))
.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark))
.setShowTitle(true)
.addDefaultShareMenuItem()
.setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left)
.setExitAnimations(this, android.R.anim.slide_in_left,
android.R.anim.slide_out_right);
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.intent.setPackage(packageName);
customTabsIntent.launchUrl(this, Uri.parse(url));从现在开始,您一定想知道如何在打开“自定义”选项卡时设置哪个包名。即使您可以对特定浏览器的包名进行硬编码,例如:
customTabsIntent.intent.setPackageName("com.android.chrome");但是,由于火狐和三星等其他浏览器也支持CustomTabs,所以理想情况下,您需要了解安装了哪些浏览器并使用其中之一。
下面的代码可以帮助实现这一点:
public static ArrayList getCustomTabsPackages(Context context) {
PackageManager pm = context.getPackageManager();
// Get default VIEW intent handler.
Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
// Get all apps that can handle VIEW intents.
List resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
ArrayList packagesSupportingCustomTabs = new ArrayList<>();
for (ResolveInfo info : resolvedActivityList) {
Intent serviceIntent = new Intent();
serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
serviceIntent.setPackage(info.activityInfo.packageName);
// Check if this package also resolves the Custom Tabs service.
if (pm.resolveService(serviceIntent, 0) != null) {
packagesSupportingCustomTabs.add(info);
}
}
return packagesSupportingCustomTabs;
}可以将它与CustomTabsClient.getPackageName结合使用来选择包。
发布于 2020-03-05 17:22:07
多亏了这里和那里,我用Kotlin写了同样的文章。感谢阿加迪普的更新。
private fun getCustomTabsPackages(context: Context, url: String): List<ResolveInfo> {
val pm: PackageManager = context.packageManager
// Get default VIEW intent handler.
val activityIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
// Get all apps that can handle VIEW intents.
val resolvedActivityList: List<ResolveInfo> = pm.queryIntentActivities(activityIntent, 0)
return resolvedActivityList.filter {
val serviceIntent = Intent()
serviceIntent.action = ACTION_CUSTOM_TABS_CONNECTION
serviceIntent.setPackage(it.activityInfo.packageName)
// Check if this package also resolves the Custom Tabs service.
pm.resolveService(serviceIntent, 0) != null
}
}
private fun showBrowser(url: String) {
val customTabsPackages = getCustomTabsPackages(context!!, url)
if (customTabsPackages.isNullOrEmpty()) {
openAnyBrowser(url)
} else {
// Get the first Chrome-compatible browser from the list.
val packageName = customTabsPackages.first().activityInfo.packageName
openChromeTabs(packageName, url)
}
}
private fun openAnyBrowser(url: String) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
if (intent.resolveActivity(context!!.packageManager) != null) {
startActivity(intent)
} else {
showError("Cannot open web browser")
}
}
private fun openChromeTabs(packageName: String, url: String) {
val params = CustomTabColorSchemeParams.Builder()
.setToolbarColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
.setNavigationBarColor(ContextCompat.getColor(context, R.color.colorPrimary))
.build()
val builder = CustomTabsIntent.Builder()
.setColorScheme(CustomTabsIntent.COLOR_SCHEME_DARK)
.setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, params)
//.setDefaultColorSchemeParams(params)
.setShowTitle(true)
.setShareState(SHARE_STATE_DEFAULT)
.setStartAnimations(context, R.anim.slide_in_right, R.anim.slide_out_left)
.setExitAnimations(context, android.R.anim.slide_in_left,
android.R.anim.slide_out_right)
val customTabsIntent = builder.build()
customTabsIntent.intent.setPackage(packageName)
customTabsIntent.launchUrl(context!!, Uri.parse(url))
}在build.gradle中
// Chrome Custom Tabs.
implementation 'androidx.browser:browser:1.3.0'在API 30 intent.resolveActivity(context!!.packageManager) 返回空中,如果您想显示其他浏览器,那么在AndroidManifest中添加以下几行:
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" />
</intent>
</queries>https://stackoverflow.com/questions/50699021
复制相似问题