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

Programmatically setting icon to navigation drawer menu item using drawable resource is always black in Android
DSP (Digital Sound Processing) with Android Media Player
AOSP layout xml file preview in eclipse
Sending a parameter from activity1 to Activity2
Android - Keep Service Running to keep Data in Storage
onCreateView is called after returning back from second activity
Cannot test app with android 5.1 device(Infinix hot note) on android studio 1.4 and 2.0
How can I fix this misbehavior of SwipeRefreshLayout
Why can't I reference the attr `android:popupEnterTransition`?
Poor recognition accuracy of Pocketsphinx using phoneme recognition on Android, French language
WeatherApp, setting an image inside and AsyncTask while parsing XML using setImageResource()
Multiple Activities, launching and passing data
Retrieve user entered data in fragment from views
OnClickListener of button in recyclerview acting on wrong view
How do I save the state of a Webview Fragment?
Android how to clear focus of edit text on closing keyboard

Categories

HOME
uml
asp.net-web-api
github-for-windows
alexa-skills-kit
vxml
character
discrete-mathematics
fosrestbundle
minecraft-forge
appcelerator-studio
alamofire
x-cart
screen-readers
aws-sdk
google-container-registry
digital-signature
sigma.js
blade
ember-cli
angular-routing
itunes
delimiter
entity-relationship-model
subclass
paragraph
imx6
dm-script
bc
ods
cin
numberpicker
attributeerror
file-sharing
berkeley-db-je
snapchat
xbmc
payara-micro
git-flow
excel-dna
er-diagram
type-theory
ampps
filenet
docpad
redbeard-ios
xcode-server
mediametadataretriever
grass
rubaxa-sortable
toolkit
evolus-pencil
zxspectrum
actionfilterattribute
mass-assignment
filesplitting
human-computer-interface
template-deduction
renderer
maven-scm
accesscontrolexception
gd-graph
gtm-oauth2
scrypt
inputmismatchexception
mongohq
android-loadermanager
panda3d
paginator
azman
predicatebuilder
hibernateexception
user-tracking
u2netdk
mysql-error-1205
lr
pylucene
timthumb
cdonts
presentation-layer
easyb
rowlex
openfaces
echo3
sqlobject
java1.4

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