0

In my code, I create a notification that once clicked tries to uninstall an app identified by its package name. I want the notification to be disappear only if the app is successfully uninstalled. If the user clicks "cancel" for app uninstall pop-up, I would like the notification to persist.

Any ideas how I can do that?

    Uri packageURI = Uri.parse("package:" + package_name);
    Intent intent = new Intent(Intent.ACTION_DELETE, packageURI);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent pintent=PendingIntent.getActivity(context,0,intent,0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentIntent(pintent);
    builder.setContentTitle(uninstallAppNotificationTitle);
    builder.setTicker(uninstallAppNotificationTicker);
    builder.setContentText(uninstallAppNotificationContent);
    builder.setWhen(System.currentTimeMillis());
    builder.setAutoCancel(true);
    builder.setDefaults(Notification.DEFAULT_ALL);
    builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
    builder.setStyle(new NotificationCompat.BigTextStyle().bigText(uninstallAppNotificationContent)) ;
    builder.setPriority(Notification.PRIORITY_MAX);
    builder.setOngoing(true);

    Notification notification = builder.build();
    notification.contentIntent = pintent;
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(TAG, uninstallNotificationId, notification);
lyc001
  • 777
  • 1
  • 10
  • 25

1 Answers1

1

There is a way. you can create a broadcast receiver for it

Package removed

I am sure you are persisting the notification id somewhere. just use that id to cancel the notification. have one uninstall id or package id fixed for each app, receive the intent in broadcast receiver and cancel the notification using Notification Manager.

Community
  • 1
  • 1
drulabs
  • 3,071
  • 28
  • 35