15

I am trying to capture download complete events, but my BroadcastReceiver is not receiving them. Here is the receiver:

public class DownloadListenerService extends BroadcastReceiver {        
    @Override
    public void onReceive(final Context context, Intent intent) {
        System.out.println("got here");
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = settings.edit();

        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            String downloadPath = intent.getStringExtra(DownloadManager.COLUMN_URI);
            editor.putString("downloadPath", downloadPath);
            editor.commit();
        }
    }
}

Here is the manifest:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

    <receiver 
        android:name="com.example.alreadydownloaded.DownloadListenerService" 
        android:exported="true">
        <intent-filter>
            <action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
        </intent-filter>
    </receiver>
 </application>

Anyone see what's wrong?

John Roberts
  • 5,885
  • 21
  • 70
  • 124

5 Answers5

18
  • Use full package name for you receiver like com.example.DownloadListenerService
  • Add android:exported="true" BroadcastReceiver can receive messages from sources outside its application.
  • Change the name of the Action in the intent-filter to android.intent.action.DOWNLOAD_COMPLETE

        <receiver 
            android:name="com.example.DownloadListenerService"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
            </intent-filter>
        </receiver>
        <uses-permission android:name="android.permission.INTERNET" />
    

The receiver only will be triggered if was registered from your application using registerReceiver(@Nullable BroadcastReceiver receiver,IntentFilter filter);

Code to enqueue Download :

DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://www.google.com/images/srpr/logo4w.png"));
dm.enqueue(request);
Simon
  • 17,223
  • 1
  • 19
  • 23
  • That also did not work. I'm going to update my question with my new code. – John Roberts Sep 13 '13 at 15:19
  • you know that you will receive broadcasts from the DownloadManager for downloads you requested from you app only, right? – Simon Sep 13 '13 at 16:16
  • I did not know that. Is there any way of capturing downloads that aren't requested from the app? – John Roberts Sep 13 '13 at 17:38
  • If you are downloading the file in an external storage you should also use the permission – Lips_coder Apr 19 '16 at 05:47
  • why should we add android:exported="true". Is it necessary even if the same application is initiating the downlod? – nizam.sp Jun 17 '17 at 19:36
  • String downloadFilePath = intent.getStringExtra(DownloadManager.COLUMN_URI); returns null. I am testing on android N 7.0. I want to get the path of file just downloaded. – Qadir Hussain Sep 23 '17 at 11:53
1

I think the action name in your XML is wrong. The docs state that the correct one is: android.intent.action.DOWNLOAD_COMPLETE not DownloadManager.ACTION_DOWNLOAD_COMPLETE - you need to use the constant, not the Java form.

<receiver android:name=".DownloadListenerService" >
    <intent-filter>
        <action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
    </intent-filter>
</receiver>
Kylar
  • 8,876
  • 8
  • 41
  • 75
0
  <receiver
        android:name=".BroadCast_Service.Download_BroadCast"
        android:exported="true">
        <intent-filter android:priority="1099">
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
        </intent-filter>
    </receiver>
PKSNAP
  • 1
  • 1
-1

I think you are calling DownloadManger service from the IntentService/Service. If so remove it from there and put it into activity.

add permission in android manifest

 <uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />
Flexo
  • 87,323
  • 22
  • 191
  • 272
AndroidLad
  • 687
  • 7
  • 14
-4

If the download is based on your app then you need to send a broadcast?

Intent i = new Intent();
i.setAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
sendBroadcast(i);
user755
  • 2,521
  • 3
  • 18
  • 29