Skip to content Skip to sidebar Skip to footer

Package_added Broadcastreceiver Doesn't Work

I have a broadcast receiver registered in Manifest:

Solution 1:

Did you run the app that contains this broadcastReceiver before installing the other apps?

Starting at some API version, broadcastReceivers will not work till you execute the app. Put an activity and execute it.

Also , don't forget to add the following into the broadcastReceiver:

<data android:scheme="package" />

EDIT: On Android 8 and above, if your app targets API 27 or more, it will work partially, so you have to register to those events in code and not in manifest. Here's a list of intents that are still safe to use in manifest: https://developer.android.com/guide/components/broadcast-exceptions.html .

The rest should be used in code. More info here

Solution 2:

Since android.intent.action.PACKAGE_ADDED is a System Intent (note that your own app will not receive it at its installation), your BroadcastReceiver will receive messages from sources outside your app. Thus, check you did NOT put: android:exported="false"

You also may need to add:

<data android:scheme="package" />

So, your BroadcastReceiver in your AndroidManifest.xml should look like this:

<application...><receiverandroid:name=".NewAppReceiver"android:exported="true"><intent-filter><actionandroid:name="android.intent.action.PACKAGE_ADDED" /><dataandroid:scheme="package" /></intent-filter></receiver></appcication>

If it still doesn't work, you may try to put an higher priority, such as: android:priority="1000"

Take a look at: http://developer.android.com/guide/topics/manifest/receiver-element.html

Solution 3:

Registering receiver from manifest would not work from API 26(android 8). Because it had performance impact on older versions.

But we can register receiver from java code and receive updates of removed and added applications.

valintentFilter= IntentFilter()
    intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED)
    intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED)
    intentFilter.addDataScheme("package")
    registerReceiver(YourBroadcastReceiver(), intentFilter)

Solution 4:

Are you trying to receive the intent in the application you are installing? The documentation for ACTION_PACKAGE_ADDED says:

Note that the newly installed package does not receive this broadcast.

Another possibility is that this intent might not be delivered to components registered via the manifest but only manually (as described in an answer by Mark Murphy to Stack Overflow question Can't receive broadcasts for PACKAGE intents).

Solution 5:

If you try to receive some other package it must be worked.

(As @Savvas noted) If you try to receive your own package's addition you can't receive it. Even if your broadcast receiver has action.PACKAGE_ADDED, receiver's onReceive method isn't triggered.

In this case your best bet is saving this data. By using sharedPreferences, add a key something like "appIsWorkedBefore", and on your launcher Activity's onCreate method set this variable as "true". And you can make your works with respect to this Boolean.

Post a Comment for "Package_added Broadcastreceiver Doesn't Work"