在Android WebView中处理外部链接(例如Gmail应用程序)确实可以 [英] Handling External Links in android WebView like Gmail App does

查看:115
本文介绍了在Android WebView中处理外部链接(例如Gmail应用程序)确实可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm a web developer. I'm currently developing android application on Android Studio using WebView which access my website as an android application. 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). Below is the example of gmail app.

电子邮件包含外部链接

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

有什么想法吗?

推荐答案

这很简单.您必须使用Gergely所建议的 Chrome自定义标签.下面是一些小的功能代码,可以帮助您实现这一目标.

It is pretty simple. You have to use Chrome Custom Tabs as suggested by Gergely as well in comment. Below is the small functional code that will help you to achieve this.

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

First add this dependency to your build.gradle(Module:app)

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

第二个将下面的函数添加到您的代码中,只需将字符串URL传递给它.

Second add below function to your code and simply pass string URL to it.

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);
}

不要担心,它会自行照顾.由于"Chrome自定义标签"可以自定义,因此可以做很多事情,就像您可以将菜单添加到工具栏一样.有关详细信息,您可以在此处访问Google本身的官方文档.

Rest it will take care itself. Since Chrome Custom Tabs can customised so lot can be done like you can add menu to toolbar. For detailed information you can visit official documentation from Google itself here.

希望它会帮助您从开始:)

Hope it will help you to start with :)

这篇关于在Android WebView中处理外部链接(例如Gmail应用程序)确实可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆