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;
}