Search code examples
androidandroid-intentbroadcastreceiver

Cannot receive Uninstall broadcast


I am trying to detect the uninstall action of my application. Till now, I have got a specific code that catch the uninstall action and inflate an Activity. Here is the code:

Manifest:

<activity
android:name=".UninstallActivity"
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>

I have created a simple Activity called UninstallActivity and It works fine. When the user try to uninstall the app this Activity has been inflated.

I am trying to listen on those intents with a Receiver instead of Activity but I have failed to get this action. My code is:

Manifest:

<receiver android:name=".PackageUninstallReceiver" >
        <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>
    </receiver>

PackageUninstallReceiver:

public class PackageUninstallReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("uTag", "In the PackageUninstallReceiver onReceive()");

    if (intent.getAction().equals(Intent.ACTION_DELETE) && intent.getDataString().contains(context.getPackageName())) {
        Log.d("uTag", "Uninstallation is being happened....");
    }
}
}

First, is it possible to listen to this Intent with the receiver? If yes, what is wrong with my code?


Solution

  • The Intent used to start an Activity (in this case, an Intent to VIEW or DELETE a PACKAGE) is a completely different thing from a braodcast Intent. They share some of the same properties, but are still completely different things. a broadcast Intent will never start an Activity and an Intent used to start an Activity will never be seen by a BroadcastReceiver.

    Therefore, the answer to your question

    First, is it possible to listen to this Intent with the receiver?

    is "no".