我有一个活动可以将外部网址加载到我的应用内的网页浏览中。我想在可用时使用Chrome自定义标签,但我支持的设备可能没有支持它们的Chrome版本。
如果不支持CustomTabs,我想使用我的旧代码,但是当它们使用CustomTabsIntent.Builder()时。旧代码将内容加载到Activity中包含的WebView中,我仍然可以在其中管理ActionBar。
我想编写一个帮助方法,告诉我它是否受支持,但我不确定如何。开发者页面上的信息非常简洁: https://developer.chrome.com/multidevice/android/customtabs
它表示如果绑定成功,则可以安全地使用自定义选项卡。有没有一种简单的方法可以绑定来测试它?
像这样我假设:
@Component
@PropertySource("classpath:application.properties")
public abstract class A {
@Value("${test.value}")
public String value;
public A(){
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
public A(String text) {
this();
// do something with text and value.. here is where I run into NPE
}
}
public class B extends A {
//addtnl code
private B() {
}
private B(String text) {
super(text)
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:META-INF/spring/application-core-test.xml",
"classpath:META-INF/spring/application-schedule-test.xml"})
@PropertySource("classpath:application-test.properties")
public class TestD {
@Value("${value.works}")
public String valueWorks;
@Test
public void testBlah() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
B b= new B("blah");
//...addtnl code
}
}
答案 0 :(得分:14)
您可以使用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());
}
请注意,其他浏览器将来可能会支持自定义标签,因此您可能需要修改它以支持此案例。
答案 1 :(得分:7)
您可以尝试使用以下代码来确定您是否拥有支持自定义标签的浏览器:
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);
}
}
答案 2 :(得分:6)
我最终在我的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;
}
答案 3 :(得分:1)
从 Chrome 的开发者网站,我找到了以下内容 -
从Chrome 45开始,Chrome自定义标签现在通常可供所有用户使用 适用于所有Chrome支持的Android版本的Chrome用户 (Jellybean起)。
链接:https://developer.chrome.com/multidevice/android/customtabs#whencaniuseit
因此,我检查 Chrome 是否支持 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
}
我不知道它有多高效,只想分享。
答案 4 :(得分: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之前,如果您遇到任何意外错误或此方法可能导致的任何其他问题,请告诉我。请提供您的反馈,我们是一个社区。 p>