Receiving Package Install and Uninstall Events

Receiving package install and uninstall events

I tried to register the BroadcastReceiver in either manifest file or java code. But both of these two methods failed to trigger the onReceive() method.
After googling this problem, I found a solution for both methods from another Thread in SO:
Android Notification App

In the manifest file (this approach no longer applies since API 26 (Android 8), it was causing performance issues on earlier Android versions):

<receiver android:name=".YourReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>

In java code:

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
intentFilter.addDataScheme("package");
registerReceiver(br, intentFilter);

This should work for you.

Call broadcast receiver at time of uninstalling application in android

You can get the broadcast for any other package getting uninstalled but never for your own package.

Reason

That happens because when you register uninstall receiver in your own app and when the app is uninstalled, the registered BroadcastReceiver has been uninstalled before the app gets uninstalled,so its own uninstallation event won't be received by that BroadcastReceiver.

Just think of a scenario that say a broadcast is registered(say a sms receiver) and the app is about to get uninstalled.Now sms comes in broadcast detects it but the broadcast's application(which created it) got uninstalled.The may lead to inconsistency in the system.So may be thats the reason it happens.

Android: BroadcastReceiver: UNINSTALL_PACKAGE intent

I need to detect when my app is being uninstalled

There is no supported way to do this, for obvious security reasons.

But it just doesn't catch it

ACTION_UNINSTALL_PACKAGE is an activity action. Nothing on an Android device should be sending it as a broadcast. Hence, you cannot listen to it via a BroadcastReceiver.

How to make my app receive broadcast when other applications are installed or removed

In your manifest:

<receiver android:name=".apps.AppListener">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
</intent-filter>
</receiver>

Add the line before the intent-filter tag

<data android:scheme="package"/>

So your manifest should look like this:

<receiver android:name=".apps.AppListener">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>

Am not sure about the PACKAGE_REMOVED intent in that if its actually is available.

How to remove the install/unistall receiver on killing the applicationfrom multitask pane/force closing the app?

You'll need to keep a reference to MyReceiver somewhere so that you can use that to unregister it. I assume that you are creating and registering MyReceiver in an Activity. If so, you should be able to unregister it in onDestroy():

context.unregisterReceiver(myReceiver);

You need to make sure that you call unregisterReceiver() on the same Context that you used to call registerReceiver().


Also, as a safety net, you should check that this.module != null in MyReceiver.onReceive(). If it is null, just ignore the call to onReceive() and do nothing. This will prevent your app from crashing.



Related Topics



Leave a reply



Submit