我知道如何在另一项活动中午餐,但在开始活动之前,如何确保我的MIME类型存在活动?例如,如果我有一个我想要显示的PDF文件,我该如何确保PDF查看器存在?
这是我用来午餐PDF查看器的代码
MimeTypeMap tMimeType = MimeTypeMap.getSingleton();
String tMimeStr = tMimeType.getMimeTypeFromExtension("pdf");
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile("xyz.pdf"), tMimeStr);
try
{
startActivity(intent);
}
catch (Exception e)
{
// Display error message here
}
答案 0 :(得分:3)
获取Intent
并将其传递给PackageManager
的{{1}}。如果您返回零长度列表,则无法处理您的queryIntentActivities()
。如果您获得包含两个或更多条目的列表,请考虑使用Intent
让用户选择要使用的活动。