2015-10-06 23 views
6

私は、アプリケーション内のWebビューに外部URLを読み込むアクティビティを持っています。 Chromeのカスタムタブは使用できるようにしたいが、サポートしているChromeのバージョンがない可能性のあるデバイスをサポートしています。ChromeでChromeのカスタムタブがサポートされているかどうかを確認するにはどうすればよいですか?

CustomTabsがサポートされていない場合は、以前のコードを使用したい場合はCustomTabsIntent.Builder()を使用してください。古いコードは、ActionBarをまだ管理できるActivityに含まれるWebViewにコンテンツを読み込みます。

私はそれがサポートされているかどうかを教えてくれるヘルパーメソッドを書いてみたいと思いますが、わかりません。開発者ページの情報はかなりスリムです: https://developer.chrome.com/multidevice/android/customtabs

あなたが成功すると、カスタムタブを安全に使うことができます。これをテストするためにバインドする簡単な方法はありますか?このよう

私が想定しています

/** 
    * 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; 
    } 

答えて

5

私はので、私はそれがサポートされていない場合はチェックして扱うことができる私のUtilsクラスの静的メソッドを書くことになりましたカスタムのタブがサポートされているかどうかを確認するには、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()); 
    } 

あなたはこのケースをサポートするために、それを変更したい場合がありますので、他のブラウザでは、将来的にカスタムタブをサポートするかもしれないことに注意してください。

+2

はクロームカスタムタブの場合、このチェックは他のブラウザでサポートされていますか? – X09

11

を代わりに:

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 
} 
+0

あなたは、クロムカスタムタブの専門家.. –

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

ファンタスティック答えているクロムタブ互換性のあるブラウザの存在を示す、単純なブール値を返すように変更することができます。 https://gist.github.com/aashreys/fd8a14​​e652b7c80b784dc90be235d208 – aashrey99

0

を、私はこの問題を解決しキャッチブロックで例外ActivityNotFoundを処理してください。

クロムのブラウザアクティビティを開始できるかどうかを確認することです。開始できない場合や例外がスローされた場合は、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クリックする前にあります。あなたのフィードバックをお寄せください、我々はコミュニティafterallです。クロームのデベロッパーサイトから

1

が、私は以下のを発見した - クロム45のよう

、クロームカスタムタブには、ChromeのサポートAndroidのバージョンのすべてに、今クロームのすべて ユーザーに一般的に利用可能である (ジェリービーンズ以降)。

リンク:。https://developer.chrome.com/multidevice/android/customtabs#whencaniuseitので

、私はクロームはバージョンによってクロームカスタムタブをサポートしているかどうかをチェックちょうど共有したい、私はそれがどのように効率的に分からない

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 
} 

は私のコードを確認してください。

関連する問題