Search code examples
androidandroid-broadcastreceiver

Handle Android App removing programmatically


I am trying to handle app removing. My AndroidManifest.xml looks like this:

<uses-permission android:name="android.permission.GET_TASKS"/>

<receiver android:name=".receivers.UninstallIntentReceiver">
    <intent-filter android:priority="0">
        <action android:name="android.intent.action.QUERY_PACKAGE_RESTART" />
            <data android:scheme="package" />
    </intent-filter>
</receiver>

UninstallIntentReceiver.java

public class UninstallIntentReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // fetching package names from extras
        LogUtils.i("HK_LOG " + this.getClass().getSimpleName(), "onReceive");

        String[] packageNames = intent.getStringArrayExtra("android.intent.extra.PACKAGES");

        if(packageNames!=null){
            for(String packageName: packageNames){
                if(packageName!=null && packageName.equals("com.betconstruct.sportsbookModule")){
                    // User has selected our application under the Manage Apps settings
                    // now initiating background thread to watch for activity
                    new UninstallActivity(context).start();

                }
            }
        }
    }
}

When apps is stopped, onReceive() method is called, but when app is running, onReceive() method is not getting called.

In this case onReceive() is called:

- in this case onReceive called.

But in this case onReceive() is not called: but in this case onReceive not called

If I click on close button (from settings, pic that I show with arrow), after that onReceive() method is called.

I feel that I must guest how to handle this action, and solve this problem.

Any kind of suggestion will help me. Thanks.


Solution

  • I am trying to handle app removing.

    I'm not sure android.intent.action.QUERY_PACKAGE_RESTART is the intent you need.

    From javadocs:

    Ask system services if there is any reason to restart the given package. The data contains the name of the package.

    This looks like an action that's needed for system services, it doesn't even have a documentation. Also note the @hide annotation, which means that this action is not exposed to public API and you shouldn't rely on that.

    The actual action you are interested in should be android.intent.action.PACKAGE_REMOVED.

    <receiver android:name=".MyReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REMOVED" />
            <data android:scheme="package"/>
        </intent-filter>
    </receiver>
    

    If I understand your question correct, you are trying to handle an action of removing an application within the same application, meaning you expect onReceive() method of BroadcastReceiver which is in Sportsbook application to be fired as soon as user removes Sportsbook application. That's not possible, because when user uninstalls your application, all your app data, apk, classes are removed, thus there doesn't exist that receiver anymore.

    If you try to detect removal of application from another application (let's say you have 2 applications, and you want to track whether user removes one of your apps), then that would make sense, and onReceive() would be called as expected.