BroadcastReceiver not receiving download complete action
Asked Answered
M

5

17

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?

Manicure answered 13/9, 2013 at 14:49 Comment(7)
are you waiting system to send a download complete event or your app.?Midway
Waiting for system. I am downloading an attachment from Gmail as my test.Manicure
could you try adding category after intent-filer in the manifest. <category android:name="android.intent.category.HOME" /> set exproted="true" and android:enabled="true" in receriver.Midway
add enable=true outside intent-filter.Midway
Try doing that in the code just for kicks:context.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));Cartier
Can you see the file downloaded in SD card? Which device are you trying this on?Inheritrix
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.Ellaelladine
M
19
  • 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);
Methodize answered 13/9, 2013 at 15:1 Comment(6)
That also did not work. I'm going to update my question with my new code.Manicure
you know that you will receive broadcasts from the DownloadManager for downloads you requested from you app only, right?Methodize
I did not know that. Is there any way of capturing downloads that aren't requested from the app?Manicure
If you are downloading the file in an external storage you should also use the permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"> </uses-permission>Untwine
why should we add android:exported="true". Is it necessary even if the same application is initiating the downlod?Dario
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.Ellaelladine
G
2

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>
Ghiberti answered 13/9, 2013 at 14:58 Comment(5)
As Simon mentioned below, you've also got the wrong path for your receiver.Ghiberti
I changed that as well. Still no luck.Manicure
And you put the full path to the DownloadListenerService? Like com.foo.DownloadListenerService?Ghiberti
try adding android:enabled="true" to your <action> definition. I updated the answer with the action XML.Ghiberti
That also did not work. I'm going to update my question with my new code.Manicure
I
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>
Institutionalism answered 15/12, 2016 at 11:16 Comment(1)
Add some explanation with answer for how this answer help OP in fixing current issueWaterfront
R
-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" />
Resh answered 13/3, 2015 at 10:49 Comment(0)
M
-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);
Midway answered 13/9, 2013 at 15:1 Comment(1)
he needs to capture download complete events. not sending a new one.Casey

© 2022 - 2024 — McMap. All rights reserved.