首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用启用CustomTabIntent的应用程序打开AppLink

使用启用CustomTabIntent的应用程序打开AppLink
EN

Stack Overflow用户
提问于 2018-06-05 11:31:09
回答 2查看 1.2K关注 0票数 1

我正试图在我的应用程序中添加CustomTabIntent以打开我的网站的网址。问题是我已经将AppLinking实现到我的应用程序中,因为它没有出现chrome,所以它被重定向到我的deeplink处理程序类,它将我的url重定向到Chrome。

我为CustomTabIntent使用了以下代码,

代码语言:javascript
复制
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的一种方式吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-07 07:21:59

是的,您可以通过访问CustomTabsIntent中的内部意图并调用setPackageName来设置要打开的应用程序的包名。

代码语言:javascript
复制
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));

从现在开始,您一定想知道如何在打开“自定义”选项卡时设置哪个包名。即使您可以对特定浏览器的包名进行硬编码,例如:

代码语言:javascript
复制
customTabsIntent.intent.setPackageName("com.android.chrome");

但是,由于火狐和三星等其他浏览器也支持CustomTabs,所以理想情况下,您需要了解安装了哪些浏览器并使用其中之一。

下面的代码可以帮助实现这一点:

代码语言:javascript
复制
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结合使用来选择包。

票数 4
EN

Stack Overflow用户

发布于 2020-03-05 17:22:07

多亏了这里那里,我用Kotlin写了同样的文章。感谢阿加迪普的更新。

代码语言:javascript
复制
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

代码语言:javascript
复制
// Chrome Custom Tabs.
implementation 'androidx.browser:browser:1.3.0'

在API 30 intent.resolveActivity(context!!.packageManager) 返回空中,如果您想显示其他浏览器,那么在AndroidManifest中添加以下几行:

代码语言:javascript
复制
<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="http" />
    </intent>
</queries>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50699021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档