androidbroadcastreceiverandroid-4.0-ice-cream-sandwich

PACKAGE_ADDED BroadcastReceiver doesn't work


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?


Solution

  • 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