This post demonstrates like to use the DownloadManager class available in Android SDK. This class is very powerful and enable us to download files from Internet to our device. Our application will have a main screen with one CheckBox to define whether our downloads will be done only if a WI-FI connection be available and one Button that starts the download. The other activity will be called when the notification area is clicked and will show the filename name and to enable cancelling the download.
You will see at this post:
- Writing in shared preferences
- Downloading a file
- File System
The code used in this post can be downloaded here.
At first, is necessary to write the permissions on the manifest file.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I believe that these permissions are very easy to understand, because their names are very deductible.
I created a BroadcastReceiver to receiver all download notifications. Purposely, I registered the receiver on the manifest, so, even that our application is closed, it will be called when the user click on the notification area. When the user clicks on the notification area, the download id is retrieved and checked whether it was started by our application.
@Override
public void onReceive(Context context, Intent intent) {
if (!checkDownloadID(context))
return;
String action = intent.getAction();
if (action == DownloadManager.ACTION_DOWNLOAD_COMPLETE)
downloadCompletedOrCancelled(context, intent);
if (action == DownloadManager.ACTION_NOTIFICATION_CLICKED)
downloadNotificationClicked(context, intent);
}
private void downloadNotificationClicked(Context context, Intent intent) {
String extraID = DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS;
long[] references = intent.getLongArrayExtra(extraID);
for (long reference : references) {
Intent intentDownloadDetails = new Intent(context,
DownloadDetailsActivity.class);
Bundle bundle = new Bundle();
bundle.putLong(General.EXTRA_DOWNLOAD_ID_KEY, reference);
intentDownloadDetails.putExtras(bundle);
intentDownloadDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentDownloadDetails);
}
}
Note that the DownloadDetailsActivity is called when the notification area is clicked by the user.
When the user starts a new download on the MainActivity, the download id is stored on the preferences. It is used when our application starts, to check if the download manager is controlling a download from our application.
The code block below is very interesting because it has a lot of important tips. I commented this code purposely.
buttonStartDownload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!checkConnection())
return;
// create a downloadManager reference and sets it...
// DownloadManager downloadManager;
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new Request(Uri.parse(url));
if (checkBoxDownloadWifiOnly.isChecked())
request.setAllowedNetworkTypes(Request.NETWORK_WIFI);
// allows that the file be scanned by media scanner...
request.allowScanningByMediaScanner();
//set the details of notification area...
request.setTitle(getResources().getText(R.string.downloadImage));
request.setDescription(getResources().getText(
R.string.nasaImageForDownload));
// saves the image on the public directory of pictures...
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_PICTURES, "image.jpg");
// disables the controls...
buttonStartDownload.setEnabled(false);
checkBoxDownloadWifiOnly.setEnabled(false);
// starts the download and use the shared preferences to persist
// its key.
long reference = downloadManager.enqueue(request);
Editor editor = preferences.edit();
editor.putLong(General.PREFERENCE_DOWNLOAD_ID_KEY, reference);
editor.apply();
// finishes the activity...
finish();
}
});
I believe that you may download the code and run it.
Good luck!
Nenhum comentário:
Postar um comentário