将Chrome自定义标签用于外部网址

时间:2018-10-22 10:22:25

标签: android android-studio chrome-custom-tabs

我正在使用来自github的模板:https://github.com/mgks/Android-SmartWebView

实际上,此应用可以选择在默认浏览器中打开除Webview网址以外的网址。现在,我想在chrome自定义标签中打开它们。

第1步:我在变量中启用了它。 静态布尔值ASWP_EXTURL = true; //使用默认浏览器而不是应用程序Webview打开外部URL

第2步:添加了依赖性。

dependencies {
...
implementation 'com.android.support:customtabs:28.0.0'
}

第3步:在MainActivity.java中添加了代码(请参阅第455行)。

//Opening external URLs in android default web browser
        } else if (ASWP_EXTURL && !aswm_host(url).equals(ASWV_HOST)) {
            //aswm_view(url,true);
Uri uri = Uri.parse(url);

// create an intent builder
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();

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

// set start and exit animations
intentBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left);
intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left,
        android.R.anim.slide_out_right);

// build custom tabs intent
CustomTabsIntent customTabsIntent = intentBuilder.build();

// launch the url
customTabsIntent.launchUrl(activity, uri);

        } else {
            a = false;
        }
        return a;
    }

步骤3;在模拟器中编译并运行。该应用程序照常打开Webview网址。但是,当单击外部URL时,它会挂起并停止。

任何人都可以引导我。

我还发现了一个错误。 第一次加载时,我的某些移动菜单无法正确显示。但是在第二次单击(打开另一个页面时)后,它就固定了。

参考: 我在Github中的问题:https://github.com/mgks/Android-SmartWebView/issues/57 https://segunfamisa.com/posts/chrome-custom-tabs 如果有人在我的问题中发现错误,请更正。

1 个答案:

答案 0 :(得分:0)

您已经添加了customtabs依赖项,可以在aswm_view()条件下将其添加到if(tab){...

   CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); 
   intentBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
   intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary)); // better if you replace this with a darker shade
   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();
   try {
      customTabsIntent.launchUrl(MainActivity.this, Uri.parse(url));
   } catch (ActivityNotFoundException e) {
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setData(Uri.parse(url));
      startActivity(intent);
   }

一旦修正了依赖项错误(如果有),我相信一切都会解决。