PACKAGE_ADDED BroadcastReceiver doesn't work

廉价感情. 提交于 2019-11-30 12:51:36

问题


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?


回答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




回答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 ...>
    <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




回答3:


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).




回答4:


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.



来源:https://stackoverflow.com/questions/10888768/package-added-broadcastreceiver-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!