Search code examples
androiddownloadbroadcastreceiver

Listen for downloads from other apps


My app should listen for downloads (from any app, for example Browser) and then, depending on some file attributes, offer some actions through Notifications. This should also work when the app is currently not running in the foreground, therefore I am doing it using a statically defined receiver (in AndroidManifest.xml). However, the app never receives the "download finished" intent. This is my code:

DownloadReceiver:

public class DownloadReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // This notification is just for testing purposes
        // to see if the DownloadReceiver works
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_check)
                        .setContentTitle("Your download has finished.")
                        .setContentText("Download finished");

        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());
    }
}

AndroidManifest.xml:

<receiver
    android:name=".DownloadReceiver">
    <intent-filter>
        <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
    </intent-filter>
</receiver>

According to some solutions on the internet, I added android:exported=true and also android:enabled=true but neither helped. I also tried to use the constant name DownloadManager.DOWNLOAD_COMPLETE in the intent filter, but this has not helped me too. Any other solutions?


Solution

  • After thorough research, I found that this is not possible for privacy reasons. There is one possibility, although it's not quite easy to implement (it involves using File Observer).