如果 chrome 浏览器被用户在其设备中禁用,Chrome 自定义选项卡会崩溃

Chrome custom tab crashes if chrome browser is disabled by the user in his device

我正在尝试在 chrome custom tab 中打开 pdf file,使用此代码可以正常工作

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse("https://docs.google.com/viewerng/viewer?url=" +pdflink));

但问题是,如果用户已从应用程序设置中禁用 chrome,并且没有其他浏览器,它就会崩溃。那么,如果它被禁用,我如何在 chrome 自定义选项卡中打开它。

提前致谢。

这是 logcat 中出现的异常。

 Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://docs.google.com/... (has extras) }

试试这个:

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();

try {
customTabsIntent.launchUrl(this, Uri.parse("https://docs.google.com/viewerng/viewer?url=" +pdflink));
} 
catch (ActivityNotFoundException e)
{
    // display message to user
}

按如下方式处理您的异常

try{
   CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
   CustomTabsIntent customTabsIntent = builder.build();
   customTabsIntent.launchUrl(this, Uri.parse("https://docs.google.com/viewerng/viewer?url=" +pdflink));
}catch(ActivityNotFoundException e){
   // Handle your exception
}

编辑

如果用户已禁用它,那么您可以要求用户从 设置 中启用它。

打开phone设置的代码

startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);

REASON这背后的原因,可以从this SO post中轻松搜索到。 由 CommonsWare

撰写

Chrome does not appear to export that activity, so it cannot be started by third-party apps.

Even if they did, that behavior could easily vary from version to version of Chrome for Android. Google does not document or support access to such activities from any of their commercial apps, let alone Chrome.