ActivityNotFoundException没有被触发到打开下载PDF文件 [英] ActivityNotFoundException is not triggered to open a downloaded PDF file
问题描述
我想下载从URL PDF格式,并也想,如果没有检测到PDF浏览器来触发抓
短语。
下面是我的code:
{尝试
startActivity(新意图(Intent.ACTION_VIEW,Uri.parse(materialPdfUrl)));
}赶上(ActivityNotFoundException E){
openDialog(的getString(R.string.error)
的getString(R.string.no_pdf_reader));
}
现在的问题是,ActivityNotFoundException永远不会触发因为它总是下载PDF即使周围没有PDF浏览器。我怎么你建议这样做吗?
编辑:
这里是我的老code:
意图pdfIntent =新意图(Intent.ACTION_VIEW,Uri.parse(materialPdfUrl));
pdfIntent.setType(应用程序/ PDF格式);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(pdfIntent);
startActivity(新意图(Intent.ACTION_VIEW,Uri.parse(materialPdfUrl)));
开始的隐式意图
,因此不会抛出 ActivityNotFoundException
。
如果你读这 http://developer.android.com/指南/组件/意图,filters.html#ccases
考虑,例如,当使用者在网页上的链接的浏览器应用程序的功能。它首先要显示的数据(因为它可以,如果该链接是一个HTML页面)。如果它不能显示数据,它放在一起的隐含意图的方案和数据类型,并尝试启动一个活动,可以做的工作。如果没有接受者,它将要求下载管理器来下载数据。即把它的内容提供者的控制下,使活动(那些有过滤器,仅举数据类型)一个潜在的更大池中可以响应
块引用>因此,如果没有PDF浏览器中发现了Android下载管理器将尝试下载该文件(而不是抛出该异常)。
如果您要查看的PDF或被告知无法查看它(而不是下载),那么你将需要查询系统中手动使用
软件包管理系统
找出如果一个应用程序将你的意图做出回应,而不仅仅是射击和遗忘。FYI
ActivityNotFoundException
将被抛出明确意图的
是这样的:意向意图=新的Intent();
intent.setComponent(新单元名(com.facebook,NewsFeedActivity.java));
startActivity(意向);```I want to download a PDF from a url and also want to trigger
catch
phrase if no PDF Viewer is detected.Here's my code:
try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(materialPdfUrl))); } catch (ActivityNotFoundException e) { openDialog(getString(R.string.error), getString(R.string.no_pdf_reader)); }
Now the problem is that ActivityNotFoundException is never triggered because it always download the PDF even if there is no PDF Viewer around. How do you suggest I do this?
EDIT: Here's my old code:
Intent pdfIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(materialPdfUrl)); pdfIntent.setType("application/pdf"); pdfIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(pdfIntent);
解决方案startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(materialPdfUrl)));
is starting an
Implicit Intent
and therefore will not throwActivityNotFoundException
.If you read this http://developer.android.com/guide/components/intents-filters.html#ccases
Consider, for example, what the browser application does when the user follows a link on a web page. It first tries to display the data (as it could if the link was to an HTML page). If it can't display the data, it puts together an implicit intent with the scheme and data type and tries to start an activity that can do the job. If there are no takers, it asks the download manager to download the data. That puts it under the control of a content provider, so a potentially larger pool of activities (those with filters that just name a data type) can respond.
Therefore if no PDF viewers are found the Android Download Manager will attempt to download the file (rather than throw that exception).
If you want to view the pdf or be told you cannot view it (rather than download) then you will need to query the system manually using the
PackageManager
to find out if an application will respond to your intent rather than just firing and forgetting.
FYI
ActivityNotFoundException
will be thrown forExplicit Intent's
something like:Intent intent = new Intent(); intent.setComponent(new ComponentName("com.facebook","NewsFeedActivity.java")); startActivity(intent);```
这篇关于ActivityNotFoundException没有被触发到打开下载PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!