5

I am trying to register a Broadcast Receiver to receive broadcast events for the package events. Following is the code and my receiver in the manifest file. The log statment never happens, but I can clearly see the same broadcast firing for "HomeLoaders" (the Launcher) debug statements. What am I missing?

public class IntentListener extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.i("INTENT LISTNER:", intent.getAction());
    }
}

<receiver android:name="IntentListener" android:enabled="true" android:exported="true">
    <intent-filter>
        <data android:scheme="package"></data>
        <action android:name="android.intent.action.PACKAGE_ADDED"></action>
        <action android:name="android.intent.action.PACKAGE_ADDED"></action>
        <action android:name="android.intent.action.PACKAGE_CHANGED"></action>
    </intent-filter>
</receiver>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
James
  • 1,754
  • 14
  • 22
  • 1
    the error can be in IntentListener instead of com.android.samples.app.IntentListener ? – Zorb Nov 24 '11 at 18:59

3 Answers3

4

It is possible that these Intents cannot be received by components registered in the manifest, but only by receivers registered in Java via registerReceiver().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Yea im pretty sure thats the case, out of curiosity why is this the case? I don't see any security implications of this? – Faisal Abid Oct 20 '09 at 18:37
  • 3
    As I commented on another SO question a day or so ago, Android does not necessarily want to start up a new component all of the time. The one case I knew of was for battery events (e.g., ACTION_BATTERY_LOW). It looks like SCREEN_OFF (and maybe SCREEN_ON) are others. If you think of it, and you get it working with registerReceiver(), chime back on this issue. I think I need to cover this topic in a blog post and/or book section, and unfortunately the list of no-manifest-receiver Intents is undocumented. – CommonsWare Oct 20 '09 at 21:10
  • 1
    I would like to see where this is documented. I have inspected the source and the only check against these protected broadcasts is so a non system process cannot initiate a broadcast. I have also seen other packages in the source register for this intent through the manifest only. – James Oct 21 '09 at 13:40
0

These three intents namely,

Intent.ACTION_PACKAGE_ADDED
Intent.ACTION_PACKAGE_REMOVED
Intent.ACTION_PACKAGE_CHANGED

when broadcasted by the system, have

Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT

flag added so that only the registered receivers will receive the broadcasts and no broadcast receiver components will be launched. Refer Intent and PackageManagerService class of source for further details.

user936414
  • 7,574
  • 3
  • 30
  • 29
0

This is my manifest, without

<category android:name="android.intent.category.DEFAULT" />

My app detects only the Android Market app install, but does not remove. Now it receives also the non-Android Market app broadcasts.

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".SomeActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="com.som.pakage.PackageInstallReceiver" >
        <intent-filter >
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.intent.action.PACKAGE_REMOVED" />

            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="package" />
        </intent-filter>
    </receiver>
</application>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zorb
  • 726
  • 11
  • 24