android


Receive download manager download complete intent and compare id - android


I am using download manager in my activity to download a file. I want to receive the ACTION_DOWNLOAD_COMPLETE intent using a broadcast receiver and match the id of the download request with the id in the intent set by download manager to check if it is the same download.
There are two options:
1) receive the intent in the activity by making a intent filter and registering a broadcast receiver. Problem here is that the broadcast receiver exists within the activity and gets destroyed with it. So if the download doesn't get completed before the activity is destroyed there is no way to receive the intent.
this.downloadId = manager.enqueue(request);
downloadID is compared with the EXTRA_DOWNLOAD_ID of the intent in onReceive method to check if it is the same download. downloadId variable is accessible inside the onReceive method.
2) receive the broadcast via manifest. Problem here is that the onReceive method does not have access to the downloadId variable set inside the activity. So I cannot check if the download is the one I need.
Am I doing it wrong? Basically I want to know if the download was completed successfully or not even if the activity/service where the download started is destroyed.
I hope my question is clear enough.
surely your first option is not good because you must do some work as soon as your download is completed so i suggest you use the second approach and store the downloadid in sharedpreferance or in a file and in onRecieve method check that by stored value. you can also use database but if you want to store just one variable (downloadid) in it, it is not a good idea because you must create table and ...
so let's see some code:
in your activity after saving your downloadid to a variable put it in a SharedPreferences file like:
SharedPreferences settings = getSharedPreferences("DownloadIDS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putLong("savedDownloadIds", your download id);
editor.commit();
and in onReceive:
#Override
public void onReceive(Context context, Intent intent) {
SharedPreferences downloadids = context.getSharedPreferences("DownloadIDS", 0);
long savedDownloadIds = downloadids.getLong("savedDownloadIds", 0);
Bundle extras = intent.getExtras();
DownloadManager.Query q = new DownloadManager.Query();
Long downloaded_id = extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
if(savedDownloadIds == downloaded_id ){ // so it is my file that has been completed
q.setFilterById(downloaded_id);
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Cursor c = manager.query(q);
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
// do any thing here
}
}
c.close();
}
}

Related Links

Retrieve data from services and display in android textview
EditText Drawable Tint not possible?
Add a image(icon) to the collapsing layout (Support library)
How to make cover flow show circularly
How to make GridView works well in landscape mode
jitpack.io failed to resolve github repo
Android - disable notifydatasetchanged for existing items
Stop Dialog from showing after orientation changes
Getting the current screen brightness
Security Exception only on Android 6
Android - How to show notification in API<11 using API
How to use search functionality from json response in android?
CardView setCardBackground Color not working in code
How do I make the jumping smooth?
Smooth curved turns with Line Control Nutiteq SDK
Unable to Update Android studio from 1.3.1 to 1.5.1

Categories

HOME
shell
uml
django
azure-stream-analytics
api
ggplot2
nvd3.js
mysql-workbench
oracle-apex-5
devise
database-normalization
tail
polling
crystal-lang
commonjs
endeca
heap
shared-libraries
mathprog
google-data-studio
imessage
kendo-mobile
dpdk
aggregation
jquery-callback
tiki-wiki
maze
licensing
ms-access-web-app
amazon-quicksight
fractions
cart
simplecv
classcastexception
keras-layer
installscript
browser-extension
openerp-7
apic
reason
sessionstorage
ods
google-data-api
resolution
dcmtk
branching-and-merging
print-css
httrack
datamaps
manova
page-replacement
magic-draw
reindex
android-testing
otp
css-transforms
pybrain
offline.js
restore
choice
cronexpression
atlas
actor-platform
signalr.client
jquery-jscrollpane
wso2developerstudio
jclouds
swizzling
html-to-pdf
cycle2
highland.js
include-guards
fuelcms
evolus-pencil
atoi
liquid-layout
avahi
cwrsync
nutiteq
zxspectrum
dot.js
angular-gettext
jbake
ssis-data-flow
paw
reflector
jquery-slider
draw2d-js
android-framework
notorm
mongohq
cffile
process-explorer
alice
joomla3.1
dajaxice
database-create
quotation-marks
cac
presentation-layer
instantiationexception
tabpanel
filesystemobject
visualj#
int64
flvplayback
technical-debt
information-management
blind

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App