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?