2015-10-06 72 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 
} 

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

相關問題