简体   繁体   English

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

[英]Handling External Links in android WebView like Gmail App does

I'm a web developer. 我是一名网络开发人员。 I'm currently developing android application on Android Studio using WebView which access my website as an android application. 我目前正在使用WebView在Android Studio上开发Android应用程序,它将我的网站作为Android应用程序访问。 One of my webpage contains many external links. 我的一个网页包含许多外部链接。 My goal is to make the android application can handle external links like Gmail App does (also like facebook and Line do). 我的目标是使Android应用程序可以处理像Gmail应用程序那样的外部链接(也像facebook和Line do)。 Below is the example of gmail app. 以下是gmail应用程序的示例。

An email contains external link 电子邮件包含外部链接

Link clicked, then application open a new activity acts like a browser without leaving Gmail application 单击链接,然后应用程序打开一个新活动,就像浏览器一样,无需离开Gmail应用程序

Any idea how to make it? 知道怎么做吗?

It is pretty simple. 这很简单。 You have to use Chrome Custom Tabs as suggested by Gergely as well in comment. 您必须按照Gergely的建议使用Chrome自定义标签以及评论。 Below is the small functional code that will help you to achieve this. 以下是可帮助您实现此目的的小功能代码。

First add this dependency to your build.gradle(Module:app) 首先将此依赖项添加到build.gradle(模块:app)

compile 'com.android.support:customtabs:23.4.0'

Second add below function to your code and simply pass string URL to it. 第二,在代码中添加以下函数,并简单地将字符串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);
}

Rest it will take care itself. 休息它会照顾自己。 Since Chrome Custom Tabs can customised so lot can be done like you can add menu to toolbar. 由于Chrome自定义标签可以自定义,因此您可以将菜单添加到工具栏中。 For detailed information you can visit official documentation from Google itself here . 有关详细信息,您可以在此处访问Google自己的官方文档。

Hope it will help you to start with :) 希望它能帮助你开始:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM