Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is android.intent.action.DOWNLOAD_COMPLETE an explicit broadcast?

My app (targetSdk=25) has a broadcast receiver defined in the manifest as follows:

<receiver android:name="my.package.DownloadManagerReceiver"
     android:exported="true">
     <intent-filter>
          <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
     </intent-filter>
</receiver>

My DownloadManagerReceiver is notified whenever Android's DownloadManager finishes downloading a file, so I can do some processing on the file that was downloaded.

I'm working on migrating my app's targetSdk to 27 (Oreo). According to https://developer.android.com/about/versions/oreo/background#broadcasts, implicit broadcast receivers registered through the manifest are not supposed to work in Android O (except for those whitelisted exceptions).

However, when I run my app using an emulator running Android 8.0 and targetSdk=27 my broadcast receiver defined in the manifest is still notified by the DownloadManager after a download completes.

I tried to find the source code where the DownloadManager sends its broadcast to understand how it sends its broadcasts but I couldn't find it.

Does anybody know whether android.intent.action.DOWNLOAD_COMPLETE is an explicit broadcast rather than an implicit one? Any ideas why my receiver is still receiving that broadcast?

like image 559
Bruno Arantes Bueno Avatar asked Aug 03 '18 13:08

Bruno Arantes Bueno


People also ask

What is explicit broadcast in Android?

An explicit broadcast is one that is targeted specifically for your application on a component that is known in advance. This happens due to the target attribute that contains the application's package name or a component class name.

What are the types of broadcast receivers in Android?

There are two types of broadcast receivers: Static receivers, which you register in the Android manifest file. Dynamic receivers, which you register using a context.

What is the difference between intent and broadcast receiver?

An intent is a messaging object, a broadcast receiver is an app component. An intent is used to request some action from some app component, it could be a broadcast receiver, an activity or a service.

What are the different types of broadcasts?

There are mainly two types of Broadcast Receivers: Static Broadcast Receivers: These types of Receivers are declared in the manifest file and works even if the app is closed. Dynamic Broadcast Receivers: These types of receivers work only if the app is active or minimized.


1 Answers

Here's what I found after digging further into the platform's source code:

1) When we instantiate the DownloadManager, it keeps a reference to the context of the app and extracts the app's package name from that context

2) That package name is inserted into the downloads database into column Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE when the download is requested

3) When the download completes, the DownloadInfo.sendIntentIfRequested() method will call Intent.setPackage() passing the package name. According to the description of method Intent.setPackage():

(Usually optional) Set an explicit application package name that limits the components this Intent will resolve to. If left to the default value of null, all components in all applications will considered. If non-null, the Intent can only match the components in the given application package.

Based on that description, my understanding is that the broadcast intent will be targeted at my app, thus working as an explicit intent.

like image 174
Bruno Arantes Bueno Avatar answered Oct 22 '22 00:10

Bruno Arantes Bueno