PACKAGE_ADDED BroadcastReceiver doesn't work
Asked Answered
A

6

12

I have a broadcast receiver registered in Manifest:

<application ...>
    <receiver android:name="com.some.pkg.NewAppReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED" />
        </intent-filter>
    </receiver>
</appcication>

And the receiver:

public class NewAppReceiver extends BroadcastReceiver {

    private static final String TAG = "NewAppReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "Intent: " + intent.getAction());
    }
}

And nothing is received when I install APK manually or from the Android Market. Why?

Abjure answered 4/6, 2012 at 21:40 Comment(0)
P
32

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

Poynter answered 4/6, 2012 at 22:32 Comment(5)
Thanks, added <data android:scheme="package" /> into intent-filter and it works!Abjure
@android developer , thanks but problem remains with PACKAGE_REMOVED while removing an app in DDMS view .Do u have any suggestionAtonic
It still does not work for me. I have multiple actions in my intent filter. Among which, PACKAGE_FULLY_REMOVED properly triggers my receiver's onCreate, however, PACKAGE_ADDED simply does not. My app is already in running state, I have exported="true" for the receiver and I have included <data android:scheme="package" /> as per your suggestion. Am I missing something here?Salado
@Salado This was written a long time ago. 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: developer.android.com/guide/components/… . The rest should be used in code. More info here: developer.android.com/about/versions/oreo/…Poynter
Thanks, @androiddeveloper, that helped.Salado
M
9

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.

    val intentFilter = IntentFilter()
    intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED)
    intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED)
    intentFilter.addDataScheme("package")
    registerReceiver(YourBroadcastReceiver(), intentFilter)
Mandell answered 24/7, 2020 at 13:11 Comment(1)
In my case I registered these intents from the code but actually didn't add the data scheme. After I added it as in this answer, it immediately started working: intentFilter.addDataScheme("package")Cameroncameroon
F
8

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 ...>
    <receiver android:name=".NewAppReceiver" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <data android: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

Formula answered 16/9, 2013 at 13:49 Comment(2)
As mentioned in the docs, the maximum priority can be 999 developer.android.com/guide/topics/manifest/…Salado
For some reason, it still does not work for me. I have multiple actions in my intent filter. Among which, PACKAGE_FULLY_REMOVED properly triggers my receiver's onCreate, however, PACKAGE_ADDED simply does not. My app is already in running state, I have exported="true" for the receiver, I have maximum priority set as 999 and I have included <data android:scheme="package" /> as per your suggestion. Am I missing something here?Salado
W
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).

Wordplay answered 4/6, 2012 at 22:3 Comment(1)
The app with receiver is started when I try to receive this intentAbjure
K
0

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.

Kotto answered 13/11, 2013 at 15:45 Comment(0)
A
0

This intent action is no longer available for applications.

This is a protected intent that can only be sent by the system.

https://developer.android.com/reference/android/content/Intent#ACTION_PACKAGE_ADDED

Anders answered 7/12, 2020 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.