1

I have been following this question but with no success, basically, I want to get notified when something is downloaded or put into Downloads folder from whatever source, e.g. downloading, putting it there by computer. The problem is, I am not receiving any response from my receiver. Here is my code:

public class DownloadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.d("TAG", "SOMETHING DOWNLOADED");
    Toast.makeText(context, "SOMETHING DOWNLOADED", Toast.LENGTH_SHORT).show();
    long receivedID = intent.getLongExtra(
            DownloadManager.EXTRA_DOWNLOAD_ID, -1L);
    DownloadManager mgr = (DownloadManager)
            context.getSystemService(Context.DOWNLOAD_SERVICE);

    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(receivedID);
    Cursor cur = mgr.query(query);
    int index = cur.getColumnIndex(DownloadManager.COLUMN_STATUS);
    if(cur.moveToFirst()) {
        if(cur.getInt(index) == DownloadManager.STATUS_SUCCESSFUL){
            // do something
            Log.d("TAG", "SOMETHING DOWNLOADED");
            Toast.makeText(context, "SOMETHING DOWNLOADED", Toast.LENGTH_SHORT).show();
        }
    }
    cur.close();
}

this is how I register it in my Application file:

final IntentFilter downloadFilter = new IntentFilter();
downloadFilter.addAction("android.intent.action.DOWNLOAD_COMPLETE");
registerReceiver(new DownloadReceiver(),downloadFilter);

and this is my Android manifest:

<receiver android:name="com.tproductions.Openit.provider.DownloadReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE" android:enabled="true" />
        </intent-filter>
    </receiver>
Lukas Anda
  • 716
  • 1
  • 9
  • 30
  • From your link: `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`. – greenapps Nov 14 '17 at 13:57
  • Oh. Did not see that. Okay so js there a way to implement my feature without periodically scanning the folder ? – Lukas Anda Nov 14 '17 at 14:10
  • Did you try FileObserver ? – greenapps Nov 14 '17 at 14:25
  • Does it work in same way ? For practical reasons, I want to be able to execute my functions whenever a download happens. – Lukas Anda Nov 14 '17 at 14:28
  • What do you mean with 'work in the same way'? The other way did NOT work. Didnt it!? – greenapps Nov 14 '17 at 14:29
  • I mean the expected behavior is I want to get notified in my app whenever a change happens in specific folder. The FileObserveer is not working on Android 6, due to known bug – Lukas Anda Nov 14 '17 at 14:31
  • If FileObserver is not working then you can make worker thread and do continuous check if any file is created or not. – Shanki Bansal Feb 19 '20 at 09:40

1 Answers1

0

I registered the Broadcast Receiver in onCreate() of the activity instead of Manifest and it is working perfectly fine. I know it should not matter and you should receive the broadcast from the manifest method as well. But you can try my method It might help you.

This is how I created the class for broadcast:

public class DownloadManagerReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    // Broadcast is Received
}

}

And this is how I registered the receiver in my activity:

        // register the broadcast receiver
    DownloadManagerReceiver receiver = new DownloadManagerReceiver();
    context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

This is called dynamic declaration and other (using manifest) is called a static declaration. You can visit this Link for more details. I hope it helps you.