Shima Erfan
Shima Erfan

Reputation: 357

onReceive does not called after download completed

I'm trying to use android downloadManager via this tutorial . I move broadCastReciever block in a single function, and below is my code:

public void downloadManager(){

    boolean is_downloaded = false;

        Log.e("error ", "1");
        receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                Log.e("error ", "2");
                String action = intent.getAction();

                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                     Log.e("error ", "3");
                     DownloadManager.Query query = new DownloadManager.Query();
                    query.setFilterById(enqueue);
                    Cursor c = dm.query(query);
                    Log.e("error ", "4");
                    if (c.moveToFirst()) {
                        Log.e("error ", "5"); 
                        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);

                         if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                            // download finished successfully
                            is_downloaded = true;
                            Log.e("error ", "6");
                        }
                    }
                }
            }
        };

       getActivity().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

     }

when I run the project, is_downloaded will remains false. and in logcat I can see just this line:

error: 1

means that the application control will never reach other Logs so

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

will never call. but the image downloads successfully. every thing seems OK, where is the problem?

Upvotes: 0

Views: 1017

Answers (2)

Vyacheslav
Vyacheslav

Reputation: 27221

Try to use <uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />

of course, do not forget this:

<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

if doesn't help try to use BroadcastListener

public class DownloadListenerService extends BroadcastReceiver {        
    @Override
    public void onReceive(final Context context, Intent intent) {
        System.out.println("got here");
       ///// your code here
        }
    }
}

`

    <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" />

`

Part of code is from here: https://stackoverflow.com/a/18789470/1979882

Upvotes: 1

sam
sam

Reputation: 113

try it through AsyncTask, keep isDownloaded false in onPreExecute() then download the content in doInBackground() and then assign true to isDownloaded in onPostExecute().

Upvotes: 0

Related Questions