1

Background

I have a broadcast receiver that declares an intent filter for a system broadcast, as shown below:

<receiver android:name=".StartAppReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

The receiver is correctly receiving broadcasts. However, according to the documentation, this receiver is exported, so other apps can send intents to this receiver.

Question

To prevent other apps from sending intents to the receiver, I can mark it as not exported (android:exported="false"). I would expect this to also prevent the system from sending intents to it, therefore preventing it from receiving the BOOT_COMPLETED broadcasts.

However, it still receives the BOOT_COMPLETED broadcasts despite not being exported, which isn't the behaviour I would expect. I've confirmed this behaviour on:

  • Sony Xperia M (Android 4.3 & Android 7.1)
  • Genymotion Emulator (Android 6)
  • Official Emulator (Android 4.0.1)

I've also tried this with the android.intent.action.MY_PACKAGE_REPLACED broadcast and got the same result.

This is the behaviour I want, but I would have expected exported="false" to prevent it from receiving system broadcasts since they're not from my application.

Is it normal that I can receive system broadcasts in broadcast receivers that are not exported? Can I rely on this behaviour?

Sam
  • 40,644
  • 36
  • 176
  • 219

3 Answers3

0

In my experiences emulators do not work as a good testing platform for calling applications. I use emulators for layout sizing and rendering GL threading. This may help you on the right track.

You can limit and find out what package called the intent and filter that out in your onReceive().

How to get the sender of an Intent?

Cheers!

Community
  • 1
  • 1
0

From - https://developer.android.com/guide/topics/manifest/receiver-element

Whether the broadcast receiver can receive messages from non-system sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by the system, components of the same application, or applications with the same user ID.

Naresh NK
  • 1,036
  • 1
  • 9
  • 16
-1

To prevent receiving broadcast just put

enabled:false

in your receiver tag

user6363583
  • 106
  • 6
  • That looks like it'll work to prevent receiving the broadcast, but I *do* want to receive the broadcast. (Note that I said `This is the behaviour I want` in the question.) I'm trying to find out if it's normal and reliable behaviour that I receive the broadcast despite having `exported=true` set. I just updated the question to try to clarify this. – Sam May 22 '16 at 06:00