Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 246
artem Avatar asked Jun 04 '12 21:06

artem


People also ask

How do I check if BroadcastReceiver is registered?

Currently there is no way to check if a receiver is registered using the receiver reference. You have to unregister the receiver and catch the IllegalArgumentException that is thrown if it's not registered. This is ugly, and a boolean method to check if it's registered would be helpful.

What file is used to register the BroadcastReceiver?

Register Broadcast: Statically (manifest-declared) - This receiver can be registered via the AndroidManifest. xml file.

What is the time limit of BroadcastReceiver in Android?

As a general rule, broadcast receivers are allowed to run for up to 10 seconds before they system will consider them non-responsive and ANR the app.

What is the use of BroadcastReceiver in Android?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.


1 Answers

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

like image 136
android developer Avatar answered Oct 11 '22 15:10

android developer