0

I am using this code on Android 10 to silently uninstall a package:

PendingIntent pendingIntent = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, new Intent("ANY_UNIQUE_NAME_WILL_DO"), 0);
Android.App.Application.Context.PackageManager.PackageInstaller.RegisterSessionCallback(this);
Android.App.Application.Context.PackageManager.PackageInstaller.Uninstall(HardCodedApplicationName, pendingIntent.IntentSender);

The uninstall works fine, but I would like to keep the app's data so it will work again after I install an older version. (Actually I would just like to downgrade the app in place, but it seems Android 10 doesn't allow that so this is my next-best idea.)

I see that adb can do an install with the -k option:

adb shell pm uninstall -k com.YOUR_PACKAGE_NAME

so is it possible to do the same with PackageManager.PackageInstaller? I have read hints about the flag DONT_DELETE_DATA, which was renamed to DELETE_KEEP_DATA - but the Uninstall(...) method doesn't accept flags as a parameter. I don't even see a "Delete" package, or at least not via the silent install/uninstall methods.

jeoffman
  • 1,303
  • 10
  • 23
  • If Xamarin does not expose this interface you have to write you own Android Java/Kotlin code. – Robert Aug 09 '21 at 15:15
  • Thanks @Robert - I don't think this is a "xamarin thing" since the Android API also does not show any "flags" parameter for Uninstall: https://developer.android.com/reference/android/content/pm/PackageInstaller#uninstall(android.content.pm.VersionedPackage,%20android.content.IntentSender) The permission is "DELETE_PACKAGES" but the method is "Uninstall" - it makes me think that some old Android API was replaced with a newer one, but without the flags. – jeoffman Aug 09 '21 at 15:33
  • The `deletePackage` method that takes the flags seem to be unofficial, but it exists: https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/java/android/content/pm/PackageManager.java#L6634 – Robert Aug 09 '21 at 15:45
  • Alternatively you can try the way via Intents, as described [here](https://stackoverflow.com/a/21854473/150978). The `Intent` class has a `setFlags` method that would allow to set the `PackageManager.DELETE_KEEP_DATA`. – Robert Aug 09 '21 at 15:58
  • Thank you once again @Robert. The last time I tried using an Intent to uninstall, it did not do it silently. I don't suppose there is a flag for that? – jeoffman Aug 09 '21 at 17:08
  • I would assume that non-system apps can never (un)install an app silently. Otherwise there would be too many possibilities for apps to misuse this feature. – Robert Aug 09 '21 at 18:12

0 Answers0