Simon Simon - 12 days ago 5
Android Question

Message not displayed when pdf reader not installed

I am having some problems with the following.

When a file is downloaded, I show a notification to the user, when he/she tabs the notification it searches for the applicable application (example PDF reader) to open the file with.

Eveything works when I tab the notification but when NO PDF reader is installed no toast message is shown to the user.

Could somebody assist me please? I know the try block is empty as I do not know exactly what comes in there to invoke the toast message.

Thank you.

EDIT: It works when I uncomment "context.startActivity(target);" but this starts the open process automatically, it should start when the user tabs the notification.

Notification code:

if (file.getName().endsWith(".pdf")) {
Intent install = openPdf(urlPath, context, mNotificationManager,
NOTIFYCATIONID);
PendingIntent pending = PendingIntent.getActivity(context, 0, install, 0);

mBuilder = new NotificationCompat.Builder(context)
.setContentTitle(appName)
.setContentText("ready to open pdf.");
mBuilder.setContentIntent(pending);
mBuilder.setSmallIcon(R.drawable.placeholder);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}


Code to open the PDF file:

public static Intent openPdf(String urlPath, Context context,
NotificationManager mNotificationManager, int NOTIFYCATIONID) {
File file = new File(urlPath);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = file.getName().substring(file.getName().lastIndexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext).toLowerCase();;

Intent target = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
target.setDataAndType(Uri.fromFile(file), type);
target.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

try {
//context.startActivity(target);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "No application found to open PDF, please install one.", Toast.LENGTH_SHORT).show();
}

mNotificationManager.cancel(NOTIFYCATIONID);
return target;
}

Answer

You shouldn't try to start an Activity to determine whether it exists. Instead you can check with PackageManager if there is Activity that can handle your Intent, without starting it.

Try one of the following methods (they are all equivalent):

PackageManager pm = context.getPackageManager();
if (pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) == null) {
    Toast.makeText(context, "No application found to open PDF, please install one.", Toast.LENGTH_SHORT).show();
}

or:

PackageManager pm = context.getPackageManager();
if (intent.resolveActivity(pm) == null) {
    Toast.makeText(context, "No application found to open PDF, please install one.", Toast.LENGTH_SHORT).show();
}

or:

PackageManager pm = context.getPackageManager();
if (pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY) == 0) {
    Toast.makeText(context, "No application found to open PDF, please install one.", Toast.LENGTH_SHORT).show();
}

And instead of cancelling the notification in openPdf you can simply return null if there is no available app and don't try to show the notification at all.

Comments