I am facing an issue regarding the how to get the device uninstall dialog listener like below , "Do you want to uninstall this app?" For this I have tried with device administrator manager but not get the desired solution. If any help from you will be much more appreciated.
1 Answers
If you want to block users from uninstalling your app, you can register as Device Admin to prevent the uninstalls (users have to unregister to be able to uninstall your app), here's an example on how to do that, from there I suppose you can restrict the opening of the Android Settings app (users have to open to unregister your app as Device Admin), here's how to do that, and the settings app package name is com.android.settings
.
Second answer on how to detect app uninstall:
I'm afraid there's no official way to detect the uninstall popup, even if you have Device administrator rights, but there is kind of a hacky way to do it, check it out here, it's not perfect and has its restrictions, but that's still better than nothing.
Original answer to how to prompt to uninstall an app:
You don't need device administrator manager stuff to prompt to uninstall your app, you can simply use an intent to launch the uninstall prompt you asked for like this:
Uri packageUri = Uri.parse("package:APP_PACKAGE_NAME");
Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
startActivity(uninstallIntent);
Note: The above intent ACTION_UNINSTALL_PACKAGE is only available after API 14 (Android 4.0), if your app targets Android P and above, you have to add the permission REQUEST_DELETE_PACKAGE to your AndroidManifest
. Also, this intent is deprecated since Android Q, in which situation you should use PackageInstaller.uninstall()
like this:
String packageName = "com.your.app.package";
Intent intent = new Intent(getActivity(), getActivity().getClass());
PendingIntent sender = PendingIntent.getActivity(getActivity(), 0, intent, 0);
PackageInstaller packageInstaller = getActivity().getPackageManager().getPackageInstaller();
packageInstaller.uninstall(packageName, sender.getIntentSender());
There's a whole bunch of restrictions though, for details refer to the documentation.

- 5,354
- 2
- 29
- 54
-
Thanks for the answer but, I want listener of the popup confirmation dialog which shown before uninstalling the application. – Hardik Pandya Apr 19 '19 at 06:18
-
You should edit your question to be more clear of your intention. Anyways, if that's what you want to do, I'm afraid there's no official way to do that, even if you have Device administrator rights, but there is kind of a hacky way to do it, check it out [here](https://stackoverflow.com/a/18816716/8170714A) – Jack Apr 19 '19 at 06:29
-
Yes you understand it perfectly, I want to restrict the user not to uninstall my app for a certain period of time while my background service is running, I have tried above link but its not helped me :( . There is one app called **(OFFTIME)** which is doing this kind of stuffs in the background, I am also finding that how they can manage the uninstaller popup in the background – Hardik Pandya Apr 19 '19 at 06:52
-
If you simply want to block users from uninstalls, you can register as [Device Admin](https://developer.android.com/guide/topics/admin/device-admin.html) to block the uninstalls (users have to unregister to be able to uninstall), then from there I suppose you can restrict the opening of the settings app (users have to open to de-register your app), [here](https://stackoverflow.com/a/4213851/8170714)'s how, the settings app package name is `com.android.settings`. – Jack Apr 19 '19 at 07:04
-
@jackz314 when I execute packageInstaller uninstall then nothing happens. I think you can't just install any app using package installer class. See this answer - https://stackoverflow.com/a/30462922/3034693 – Sudhir Singh Khanger Jun 03 '20 at 06:37