我正在尝试在我的应用程序中添加CustomTabIntent来打开我的网站的网址。问题是我已经将AppLinking实现到我的应用程序中,因为Chrome Tab没有出现,它被重定向到我的深层链接处理程序类,它将我的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?
答案 0 :(得分:2)
是的,您可以通过访问CustomTabsIntent中的内部Intent并调用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");
但是,由于其他浏览器(如Firefox和Samsung)也支持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结合使用来选择包。
答案 1 :(得分:0)
感谢andreban here和there,我在Kotlin中写了同样的东西。感谢Arghadip更新。
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)
returns null中,因此,如果要显示其他浏览器,请在AndroidManifest
中添加以下行:
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" />
</intent>
</queries>