如果用户在其设备中停用了 Chrome 浏览器,Chrome 自定义标签页就会崩溃



>我正在尝试在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自定义选项卡中打开它。

提前谢谢。

这是日志猫中显示的异常。

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
}

编辑

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

打开手机设置的代码

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

这背后的原因,可以很容易地从这个SO帖子中搜索到。 作者:CommonsWare

Chrome 似乎不会导出该活动记录,因此无法由第三方应用启动。

即使他们这样做了,这种行为也很容易因Android版Chrome版本而异。谷歌不会记录或支持从其任何商业应用程序访问此类活动,更不用说Chrome了。

最新更新