2015-10-06 71 views
6

我有一个活动,将外部网址加载到我的应用内的网页视图中。我希望在可用时使用Chrome自定义标签,但我支持的设备可能没有支持它们的Chrome版本。如何检查Chrome是否支持Chrome自定义标签?

在CustomTabs不被支持的情况下,我想使用我的旧代码,但是当它们使用CustomTabsIntent.Builder()时。旧代码将内容加载到Activity中的WebView中,我仍然可以管理ActionBar。

我想写一个帮助器方法,告诉我它是否被支持,但我不知道如何。在开发者页面上的信息是非常苗条的: https://developer.chrome.com/multidevice/android/customtabs

它说如果你绑定成功的自定义标签可以安全地使用。有没有简单的方法来绑定来测试这个?

喜欢我假设:

Intent serviceIntent = new Intent("android.support.customtabs.action.CustomTabsService"); 
serviceIntent.setPackage("com.android.chrome"); 
boolean customTabsSupported = bindService(serviceIntent, new CustomTabsServiceConnection() { 
      @Override 
      public void onCustomTabsServiceConnected(final ComponentName componentName, final CustomTabsClient customTabsClient) {} 

      @Override 
      public void onServiceDisconnected(final ComponentName name) {} 
     }, 
     Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY); 

if (customTabsSupported) { 
    // is supported 
} 

回答

5

我最后写在我的Utils类的静态方法,所以我可以检查和处理情况,不支持的情况:

/** 
    * Check if Chrome CustomTabs are supported. 
    * Some devices don't have Chrome or it may not be 
    * updated to a version where custom tabs is supported. 
    * 
    * @param context the context 
    * @return whether custom tabs are supported 
    */ 
    public static boolean isChromeCustomTabsSupported(@NonNull final Context context) { 
     Intent serviceIntent = new Intent("android.support.customtabs.action.CustomTabsService"); 
     serviceIntent.setPackage("com.android.chrome"); 

     CustomTabsServiceConnection serviceConnection = new CustomTabsServiceConnection() { 
      @Override 
      public void onCustomTabsServiceConnected(final ComponentName componentName, final CustomTabsClient customTabsClient) { } 

      @Override 
      public void onServiceDisconnected(final ComponentName name) { } 
     }; 

     boolean customTabsSupported = 
       context.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY); 
     context.unbindService(serviceConnection); 

     return customTabsSupported; 
    } 
+2

此检查是否在其他浏览器中支持Chrome自定义选项卡? – X09

11

相反绑定和解除绑定服务,您可以使用PackageManager来检查自定义选项卡是否受支持。

private static final String SERVICE_ACTION = "android.support.customtabs.action.CustomTabsService"; 
    private static final String CHROME_PACKAGE = "com.android.chrome"; 

    private static boolean isChromeCustomTabsSupported(@NonNull final Context context) { 
     Intent serviceIntent = new Intent(SERVICE_ACTION); 
     serviceIntent.setPackage(CHROME_PACKAGE); 
     List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentServices(serviceIntent, 0); 
     return !(resolveInfos == null || resolveInfos.isEmpty()); 
    } 

请注意,其他浏览器将来可能会支持自定义选项卡,因此您可能需要修改该选项以支持此情况。

+0

你是Chrome自定义选项卡专家.. –

6

你可以试试下面的代码搞清楚,如果你有一个支持自定义选项卡浏览器:

private static final String TAG = "CustomTabLauncher"; 
static final String STABLE_PACKAGE = "com.android.chrome"; 
static final String BETA_PACKAGE = "com.chrome.beta"; 
static final String DEV_PACKAGE = "com.chrome.dev"; 
static final String LOCAL_PACKAGE = "com.google.android.apps.chrome"; 
String mPackageNameToUse; 

private String getPackageName(Context context) { 
    if (mPackageNameToUse != null) { 
     return mPackageNameToUse; 
    } 

    // Get default VIEW intent handler that can view a web url. 
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.test-url.com")); 

    // Get all apps that can handle VIEW intents. 
    PackageManager pm = context.getPackageManager(); 
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0); 
    List<String> packagesSupportingCustomTabs = new ArrayList<>(); 
    for (ResolveInfo info : resolvedActivityList) { 
     Intent serviceIntent = new Intent(); 
     serviceIntent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION); 
     serviceIntent.setPackage(info.activityInfo.packageName); 
     if (pm.resolveService(serviceIntent, 0) != null) { 
      packagesSupportingCustomTabs.add(info.activityInfo.packageName); 
     } 
    } 

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents 
    // and service calls. 
    if (packagesSupportingCustomTabs.isEmpty()) { 
     mPackageNameToUse = null; 
    } else if (packagesSupportingCustomTabs.size() == 1) { 
     mPackageNameToUse = packagesSupportingCustomTabs.get(0); 
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) { 
     mPackageNameToUse = STABLE_PACKAGE; 
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) { 
     mPackageNameToUse = BETA_PACKAGE; 
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) { 
     mPackageNameToUse = DEV_PACKAGE; 
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) { 
     mPackageNameToUse = LOCAL_PACKAGE; 
    } 
    return mPackageNameToUse; 
} 

致电时,你可以做这样的事情:

public void openCustomTab(Uri uri, Activity activity) { 
    //If we cant find a package name, it means there's no browser that supports 
    //Chrome Custom Tabs installed. So, we fallback to the default browser 
    if (getPackageName(activity) == null) { 
     activity.startActivity(new Intent(Intent.ACTION_VIEW, uri)); 
    } else { 
     CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); 
     intentBuilder.enableUrlBarHiding(); 
     intentBuilder.setToolbarColor(activity.getResources().getColor(R.color.purple_a_01)); 

     CustomTabsIntent customTabsIntent = intentBuilder.build(); 
     customTabsIntent.intent.setPackage(mPackageNameToUse); 

     customTabsIntent.launchUrl(activity, uri); 
    } 
} 
+0

梦幻般的答案,可以修改为返回一个简单的布尔值,指示Chrome Tab兼容浏览器的存在。 https://gist.github.com/aashreys/fd8a14​​e652b7c80b784dc90be235d208 – aashrey99

0

我解决了这个问题通过处理catch块中的ActivityNotFound异常。

诀窍是检查浏览器的Chrome浏览器活动是否可以启动,如果无法启动或抛出异常,只需通过Intent.ACTION_VIEW打开链接。

这里是所有相关的代码....

private void onBtnLinkClicked(View v, int pos) { 
    try { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
      openCustomTab(url); 
     } else { 
      openBrowserActivity(url); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     openBrowserActivity(url); 
    } 
} 

private void openBrowserActivity(String url) { 
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
    context.startActivity(browserIntent); 
} 

什么是openCustomTab(url)你说: 这里是它的相关代码。

private void openCustomTab(String url) { 
    CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); 

    int color = context.getResources().getColor(R.color.colorPrimary); 
    intentBuilder.setToolbarColor(color); 
    intentBuilder.setShowTitle(true); 

    String menuItemTitle = context.getString(R.string.menu_title_share); 
    PendingIntent menuItemPendingIntent = createPendingShareIntent(); 
    intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent);  

    intentBuilder.setStartAnimations(context, 
      R.anim.slide_in_right, R.anim.slide_out_left); 
    intentBuilder.setExitAnimations(context, 
      android.R.anim.slide_in_left, android.R.anim.slide_out_right); 

    CustomTabActivityHelper.openCustomTab(
      activity, intentBuilder.build(), Uri.parse(url), new WebviewFallback()); 
} 

我的回答的风格看起来臭屁但点击downvote让我知道,如果你遇到了任何意外的错误或任何其他问题,这种做法可能导致之前。请给出您的反馈意见,我们是一个社区。

1

的开发者网站,我发现了以下 -

自Chrome 45中,Chrome的自定义选项卡现已全面上市,给所有 用户的Chrome,在所有Chrome的支持的Android版本 的(Jellybean起)。

链接:https://developer.chrome.com/multidevice/android/customtabs#whencaniuseit

所以,我检查铬是否通过版本支持Chrome的自定义选项卡

检查我的代码:

String chromePackageName = "com.android.chrome"; 
int chromeTargetVersion = 45; 

boolean isSupportCustomTab = false; 
try { 
    String chromeVersion = getApplicationContext().getPackageManager().getPackageInfo(chromePackageName, 0).versionName; 
    if(chromeVersion.contains(".")) { 
     chromeVersion = chromeVersion.substring(0, chromeVersion.indexOf('.')); 
    } 
    isSupportCustomTab = (Integer.valueOf(chromeVersion) >= chromeTargetVersion); 
} catch (PackageManager.NameNotFoundException ex) { 
} catch (Exception ex) { } 

if (isSupportCustomTab) { 
    //Use Chrome Custom Tab 
} else { 
    //Use WebView or other Browser 
} 

我不知道它是多么有效,想和大家分享。

相关问题