像Gmail应用程序一样处理android WebView中的外部链接

Dik*_*ika 10 android webview

我是一名网络开发人员.我目前正在使用WebView在Android Studio上开发Android应用程序,它将我的网站作为Android应用程序访问.我的一个网页包含许多外部链接.我的目标是使Android应用程序可以处理像Gmail应用程序那样的外部链接(也像facebook和Line do).以下是gmail应用程序的示例.

一封电子邮件包含外部链接

点击链接,然后应用程序打开一个新活动,就像浏览器一样,无需离开Gmail应用程序

知道怎么做吗?

Gee*_*eek 12

这很简单.您必须按照Gergely的建议使用Chrome自定义标签以及评论.以下是可帮助您实现此目的的小功能代码.

首先将此依赖项添加到build.gradle(Module:app)

compile 'com.android.support:customtabs:23.4.0'
Run Code Online (Sandbox Code Playgroud)

第二,在代码中添加以下函数,并简单地将字符串URL传递给它.

private void redirectUsingCustomTab(String url)
{
    Uri uri = Uri.parse(url);

    CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();

    // set desired toolbar colors
    intentBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
    intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));

    // add start and exit animations if you want(optional)
    /*intentBuilder.setStartAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left,
            android.R.anim.slide_out_right);*/

    CustomTabsIntent customTabsIntent = intentBuilder.build();

    customTabsIntent.launchUrl(activity, uri);
}
Run Code Online (Sandbox Code Playgroud)

休息它会照顾自己.由于Chrome自定义标签可以自定义,因此您可以将菜单添加到工具栏中.有关详细信息,您可以在此处访问Google自己的官方文档.

希望它能帮助你开始:)