Search code examples
androidandroid-notificationsnotificationmanager

Install application only on notification tab


I show a notification to the user once an application has been downloaded.

I also have a installApk method which installs the downloaded application but in the code piece (completeNotification) the application gets automatically installed. I need it to only install once the user tabs the notification.

Did I overlooked something?

Thank you for your help.

The completeNotification method:

public void completeNotification() {
        Intent install = installApk(urlPath, context, mNotificationManager,
                NOTIFYCATIONID);
        PendingIntent pending = PendingIntent.getActivity(context, 0, install, 0);

        mBuilder = new NotificationCompat.Builder(context)
                .setContentTitle(appName)
                .setContentText("ready to install.");
        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(1, mBuilder.build());
    }

The installApk method:

public static Intent installApk(String urlPath, Context context,
                                    NotificationManager mNotificationManager, int NOTIFYCATIONID) {
        Intent apkIntent = new Intent();
        apkIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        apkIntent.setAction(android.content.Intent.ACTION_VIEW);
        File apkFile = new File(urlPath);
        Uri uri = Uri.fromFile(apkFile);
        apkIntent
                .setDataAndType(uri, "application/vnd.android.package-archive");
        context.startActivity(apkIntent);
        mNotificationManager.cancel(NOTIFYCATIONID);
        return apkIntent;
    };

Solution

  • remove this line:

    context.startActivity(apkIntent);