Reputation: 59
my problem is, i need to get back the finished download file to do some commands after it finishes to download.
if i do
doDownload("www.google.com","google1.html");
doDownload("www.google.com","google2.html");
the Log.d("ainfo", uriString);
still keep return the name of the second Download
output of LogCat:
D/ainfo(27946): file:///storage/emulated/0/test/google2.html
D/ainfo(27946): file:///storage/emulated/0/test/google2.html
why not google1.html and google2.html? in this way i cant manipulate files when they finish in order :(
download CODE:
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
// while(i < c.getColumnCount()) {
// Log.d("Ainfo", c.getColumnName(i) + "---" + c.getString(i));
// i++;
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Log.d("ainfo", uriString);
//AlertDialog.Builder builder = new AlertDialog.Builder(main);
//builder.setTitle("Download(s) Efetuado(s)");
//StringBuilder sb = new StringBuilder();
//sb.append("/n");
//builder.setMessage(sb.toString());
//alerta = builder.create();
//alerta.show();
}
}
}
}
};
public void doDownload(String link, String filename) {
this.dm = (DownloadManager) main.getSystemService(main.DOWNLOAD_SERVICE);
File direct = new File(Environment.getExternalStorageDirectory()
+ subDirHTMLs);
if (!direct.exists()) {
direct.mkdirs();
}
File arquivoexistente = new File(Environment.getExternalStorageDirectory()
+ subDirHTMLs + filename);
if(!arquivoexistente.exists()) {
Request request = new Request(
Uri.parse(link));
enqueue = dm.enqueue(request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle(filename)
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir(subDirHTMLs, filename));
} else {
arquivoexistente.delete();
doDownload(link, filename);
}
}
Upvotes: 1
Views: 2337
Reputation: 59
Solved adding this after the check SUCESSFUL
if(downloadId == c.getInt(0)) {
Log.d("ainfo", c.getString(c.getColumnIndex("local_uri")));
}
so now i can go to the next step!!
Upvotes: 1