How can an app detect that it's going to be uninstalled?

Barmaley picture Barmaley · Sep 9, 2013 · Viewed 73k times · Source

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.

Answer

AnniJais picture AnniJais · Sep 15, 2013

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: