1

嗨,我一直在查看我的代码的各个部分,试图找出发生了什么,但似乎无法弄清楚。以下代码应该下载两个文件,一个名为“clientraw”,一个名为“clientrawextra”,但由于某种原因,当我查看目录时,每个文件都有 2 个版本“clientraw ...1 ...”“clientrawextra ……1……”

因此,它似乎多次下载文件,我不知道为什么?

提前致谢!

    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));

和广播接收器...

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); 
        }
    }
}
4

1 回答 1

2

因此,如果有人遇到同样的问题,显然这是下载管理器的一个持续存在且尚未解决的问题。如果您的流程与我的流程相似,我已经使用了一些您可能想要使用的工作。基本上每次用户打开应用程序时,两个文件都会自动下载到 SD 卡中,这会覆盖之前下载的两个文件。所以我所做的只是添加了几个额外的功能来删除重复项......

 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);
                 } 
于 2013-09-19T14:09:47.913 回答