سؤال

Hi i've been looking through various parts of my code to try and find out whats happening but can't seem to figure it out. The following code is supposed to be downloading two files one called "clientraw" and one 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 like it's downloading the files multiple 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); 
        }
    }
}
هل كانت مفيدة؟

المحلول

So if anyone has the same issue, apparently this is an ongoing and as of yet unresolved issue with the Download manager. I have used a little bit of a work around which you might want to use if your flow is similar to mine. Basically every time that the user opens the app two files are automatically downloaded to the SD card which overwrites the two files previously downloaded. So all i did was add a couple of extra functions to delete the 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);
                 } 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top