This question already has an answer here:
All we know that usual (in practice any) antivirus application before uninstall used to fire simple dialog like: "You're going to uninstall app, are you sure?" - "yes/no".
Yes, I know that I can intercept package delete intent using intent-filter like:
<activity
android:name=".UninstallIntentActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.DELETE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
</activity>
But problem is in simple fact that this intercepts any delete requests and moreover this will fire chooser dialog between my app and stock installer. So if user will select stock installer - I won't be able to do anything.
My goal is not to prevent user from uninstalling of my app, but just rollback changes made by my app.
Learning from those antivirus apps I see that this kind of operation is possible, so please help me and explain how it is possible?
Update
Since there are some guys who doesn't believe that it's real - I would refer to Avast Mobile Security:
Anti-Theft protects itself from uninstall by disguising its components with various self-preservation techniques.
Another example: Kaspersky Internet Security for Android - here's special procedure for uninstalling it, which requires entering of secret code.
Anyway it means that there's way to intercept uninstallation procedure in order to either prevent uninstall or do some finalizing job.
In order to make your app persist you'll need to have a rooted device and be able to install it to the system partition. Once it's on there you can uninstall the updates, since they are saved along side non-system apps, but it's not as cut and dry to uninstall it from the system.
I know some of them will also save a little bit of data on the system partition just in case the devices is factory reset, but there are also ways to get the package manager to leave behind your saved data in the event that it is just uninstalled.
Another option would be to register it as a device administrator. Once you do that they will be unable to uninstall it unless they manually remove it's admin status.
<item name="android.permission.ACCESS_SUPERUSER" />
Here it looks like they're using root as well as other methods. Short of making some crazy elaborate service, which it appears they may have, there is no legitimate way to do this any other way.
Taking advantage of root is almost standard practice for AV/security apps like this, without it they don't have any real authority over any other apps so they're very limited. I think the SuperUser permission isn't shown unless you have it installed either, so many people are still unaware it's an option.
Okay. I have been investigating a lot on this problem since 2 days and finally found a "wild way" to solve it without rooting the device :)
First, here are the highlights to achieve the solution:
1. Whenever user goes to Settings -> Manage Apps -> Selects a particular application we receive a broadcast android.intent.action.QUERY_PACKAGE_RESTART with name of the application's package as extras.
2. After that when we click on the Uninstall button (with package installer), it opens an activity named - com.android.packageinstaller.UninstallerActivity
Control flow will be like:
Under App Settings the User Clicks on Uninstall button ---> We get control to show a dialogue / start another activity / etc ---> We finish our Pre-Uninstallation task ---> User is Returned back to Uninstallation confirmation screen ---> User confirms and uninstalls the app
Used Method:
We will implement a BroadcastReceiver in our application for listening the action "android.intent.action.QUERY_PACKAGE_RESTART" and match our package name inside onReceive() method. If the broadcast was received for selection of our desired application package, then we'll initiate a background thread that will keep monitoring the foreground running activities using the ActivityManager.
Once we find the foreground activity to be "com.android.packageinstaller.UninstallerActivity", it'll be confirm that user wants to uninstall our application. At this point we'll perform the desired tasks (either display a dialogue, or start another activity overlapping the uninstallation window, etc..) that are to be performed before uninstallation. After performing our task, we'll allow the user to continue with confirming the uninstallation process.
Implementation / Source Code:
In manifest.xml
add permission:
and broadcast receiver:
UninstallIntentReceiver.java (broadcast receiver class)
ListenActivities class - for monitoring the foreground activities
Known Limitations:
When the user clicks on the Uninstall button under Manage Apps settings, we'll perform our pre-uninstallation tasks and then promt the user to the Confirmation window where user can either confirm to uninstall or can Cancel the operation.
The approach described above is as of now not covering the case if user clicks on Cancel button after we have performed our task. But this could be tackled easily with some ammendments.
E.g.: We can implement a logic to revert the changes we made if the broadcast "android.intent.action.PACKAGE_REMOVED" was not received in the end.
I hope this approach will be helpful to you :) As this is the only way in my opinion we can solve your problem without rooting the device!
[Update 1]: Suggested Approach to check if the Uninstallation task was Canceled:
Its kind of funny that I had entirely different and much complex idea earlier(involving broadcasts, ActivityManager, etc.. etc..), but while writing it here just another idea struck into my mind which is comparatively very simple :)
When the User clicks on Uninstall button under Manage Apps settings and after you have performed your pre-uninstallation tasks, you just set some SharedPreference in your app that you have performed the pre-uninstall tasks and are ready for uninstallation. After this you need not to care about anything.
If the user continues to uninstall -> its well and good as you have already performed required tasks.
While if user finally clicks on Cancel button and goes away -> don't bother. Until the user goes and run your application again. Now inside "onStart()" / "onResume()" of your application's main activity, you can check the SharedPreference's value and if it was set for uninstallation, that will mean that user didn't finally proceeded with the uninstallation. And now you could revert the changes made earlier(reversing the pre-uninstall tasks performed) to ensure that your application runs perfectly!
It is simply not possible in Android
There's no way for your application to know that it is being uninstalled (without modifying the kernel). All files created in the data/data/your.app.package is deleted automatically upon install.
Another approach could be to have another application that checks whether this application is installed or not. If not, it can do the clean-up work.
UPDATE
The ACTION_PACKAGE_REMOVED intent will be sent out to all receivers except for your own. This is confirmed HERE.
UPDATE 2
Just Another thought.
as I searched for this on I found that, this can be done by monitoring logcat for your application here is a sample logcat monitor
Good thing is that to monitor logcat for same application we need not have a rooted device
and as we read each entry in logcat we can search for following string
as this event is received we know that our app is now going to be un installed
Did not try though
Again monitoring logcat is not allowed from Android Jellybean
Up to Android 5.0 there was option for detecting app uninstall is using native code:
You need to monitor for Your directory using
inotify
framework in forked process. When it's deleted You can run some system command eg.am
command that startsIntent
PoC of such solution: https://github.com/pelotasplus/ActionAfterUninstall/blob/master/app/src/main/jni/hello-jni.c