I know how to lunch another activity, But how do I meke sure an activity exist for my MIME type before starting the activity? for example if I have a PDF file that I want to display, how do I make sure a PDF viewer exist?
here is the code I use to lunch the PDF viewer
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开发者_C百科 here
}
Take the Intent
and pass it to PackageManager
's queryIntentActivities()
. If you get a zero-length list back, there is nothing that can handle your Intent
. If you get a list with two or more entries, consider using Intent.createChooser()
to give the user a choice of what activity to use.
精彩评论