【问题标题】:Handling External Links in android WebView like Gmail App does像 Gmail 应用一样处理 android WebView 中的外部链接
【发布时间】:2017-03-14 07:19:58
【问题描述】:

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

An email contains external link

Link clicked, then application open a new activity acts like a browser without leaving Gmail application

知道怎么做吗?

【问题讨论】:

  • 你需要Chrome Custom Tabs
  • 嗨@GergelyKőrössy,谢谢你的帮助。我会试试的。
  • @Dika 请查看演示代码here

标签: android webview


【解决方案1】:

这很简单。您必须按照 Gergely 的建议以及评论中的建议使用 Chrome Custom Tabs。下面是可以帮助您实现这一目标的小功能代码。

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

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

第二次将以下函数添加到您的代码中,然后将字符串 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);
}

休息它会照顾好自己。由于 Chrome 自定义选项卡可以自定义,因此可以完成很多工作,就像您可以将菜单添加到工具栏一样。有关详细信息,您可以访问 Google 本身的官方文档here

希望它能帮助你开始:)

【讨论】:

  • 不,您只需要传递托管在服务器上的 html 页面的 URL。
  • 如果用户没有安装chrome浏览器,有什么需要担心的吗?我的意思是它甚至可以在没有安装 chrome 的情况下工作吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-26
  • 2018-11-11
  • 2016-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多