Run the same file twice?

Hi, I was looking through various parts of my code to try to figure out what is happening, but it seems I cannot figure it out. The following code should load two files, one of which is called “clientraw”, and one is called “clientrawextra”, but for some reason, when I look in the directory, there are 2 versions of each file “clientraw ... 1 ..." "clientrawextra ... 1 ..."

Therefore, it seems that it downloads files several times, and I have no idea why?

Thanks in advance!

distance dis = new distance(); dis.findURL(value); String url = dis.findURL(value); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); // in order for this if to run, you must use the android 3.2 to compile your app if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { request.allowScanningByMediaScanner(); } request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "clientraw.txt"); // get download service and enqueue file DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(request); ///////////////////////////////////////////////////// distance disextra = new distance(); disextra.findextra(value); String urlextra = disextra.findextra(value); DownloadManager.Request requestextra = new DownloadManager.Request(Uri.parse(urlextra)); // in order for this if to run, you must use the android 3.2 to compile your app if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { requestextra.allowScanningByMediaScanner(); } requestextra.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "clientrawextra.txt"); manager.enqueue(requestextra); mDownload = new DownLoadComplte(); registerReceiver(mDownload, new IntentFilter( DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 

And the broadcast receiver ...

 private class DownLoadComplte extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equalsIgnoreCase( DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { Intent myIntent = new Intent(splash.this, MainActivity.class); myIntent.putExtra("key", value); //Optional parameters myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //unregisterReceiver(mDownload); splash.this.startActivity(myIntent); } } } 
+1
source share
1 answer

So, if someone has the same problem, obviously this is a persistent and as yet unresolved problem with the download manager. I used a little work that you might want to use if your thread is like mine. Basically, every time the user opens the application, two files are automatically downloaded to the SD card, which overwrites the two previously downloaded files. So all I did was add some extra features to remove duplicates ...

  File sdCard = Environment.getExternalStorageDirectory(); File file = new File(sdCard.getAbsolutePath() + "/Download/", client); Log.d("file path", String.valueOf(file)); if(file.exists()) { boolean flag = file.delete(); Log.d("file", "file deleted " + flag); } File sdCardextra = Environment.getExternalStorageDirectory(); File fileextra = new File(sdCardextra.getAbsolutePath() + "/Download/", clientextra); boolean exist = fileextra.exists(); Log.d("the file exists = ", String.valueOf(exist)); if(fileextra.exists()) { boolean flag = fileextra.delete(); Log.d("file", "file deleted " + flag); } File sdCard2 = Environment.getExternalStorageDirectory(); File file2 = new File(sdCard2.getAbsolutePath() + "/Download/", "clientraw-1.txt"); Log.d("file path", String.valueOf(file2)); if(file2.exists()) { boolean flag = file2.delete(); Log.d("file", "file deleted " + flag); } File sdCardextra3 = Environment.getExternalStorageDirectory(); File fileextra3 = new File(sdCardextra3.getAbsolutePath() + "/Download/", "clientrawextra-1.txt"); boolean exists = fileextra3.exists(); Log.d("the file exists = ", String.valueOf(exists)); if(fileextra3.exists()) { boolean flag = fileextra3.delete(); Log.d("file", "file deleted " + flag); } 
+2
source

All Articles