如果用户在设备中禁用Chrome浏览器,则Chrome自定义选项卡将崩溃。

3

我正在尝试在Chrome自定义标签页中打开PDF文件,以下代码可正常使用:

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被禁用,我该如何在自定义Chrome选项卡中打开它。
提前致谢。
这是在日志中显示的异常。
 Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://docs.google.com/... (has extras) }

哪个崩溃了? - Rahul Khurana
我说过,如果用户禁用了Chrome浏览器并且没有其他浏览器,则此代码会使应用程序崩溃。 - Anmol317
1
那个崩溃有没有名字?比如空指针异常之类的。 - Rahul Khurana
see my updated question - Anmol317
我回复了你的帖子。 - Rahul Khurana
2个回答

5

试试这个:

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
}

我知道如何处理异常,但我想知道如何在自定义选项卡中打开我的PDF文件而不使用Chrome浏览器。 - Anmol317
3
你可以使用 WebView 打开 PDF 文件。 - Vishal Yadav
1
@Vishal 非常有帮助 - 点赞!;) - akhilesh0707

1
处理您的异常,如下所示。
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);

原因 可以从 this SO post 中轻松搜索到。由 CommonsWare 编写。

Chrome似乎没有导出该活动,因此第三方应用程序无法启动它。

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


我知道如何处理异常,但我想知道如何在自定义选项卡中打开我的PDF文件而不使用Chrome浏览器。 - Anmol317
好的,但我正在寻找其他的选择。无论如何,谢谢。 - Anmol317
我知道你会问这个问题,我做了一些研究并发现这篇SO帖子很有用。 - Rahul Khurana
那个链接指向如何打开Chrome设置,但我不能要求用户打开设置并启用应用程序,只是为了在我的应用程序中打开一个页面。因此,我正在寻找其他替代方法。顺便说一句,感谢您的努力。 - Anmol317

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接