0

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;
    }
Simon
  • 1,691
  • 2
  • 13
  • 28

1 Answers1

2

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.

Marcin Jedynak
  • 3,697
  • 2
  • 20
  • 19
  • Thx Marcin, I get an error saying, resolveActivity (PackageManager) cannot be applied to (PackageManager, int). I placed your code in the openPDF method. (target.resolveActivity(pm, PackageManager.MATCH_DEFAULT_ONLY) == null) – Simon Nov 20 '16 at 11:01
  • Yes, sorry. The method should be called on pm not on intent. I updated my answer. – Marcin Jedynak Nov 20 '16 at 11:04
  • And since several equivalent approaches are mentioned in answers to similar questions, I just listed them all. – Marcin Jedynak Nov 20 '16 at 11:11
  • The code should run when the user tabs the notification and not automatically. If I`m not mistaken yours runs automatically. Same result as when I uncomment "context.startActivity(target);" . Or? That the tricky part. See the EDIT in my question. – Simon Nov 20 '16 at 11:12
  • My mistake Marcin, many thx I misunderstood. Answer accepted. :) – Simon Nov 20 '16 at 11:19