Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 依次下载和解压缩文件 脚本:_Java_Android_Android Download Manager - Fatal编程技术网

Java 依次下载和解压缩文件 脚本:

Java 依次下载和解压缩文件 脚本:,java,android,android-download-manager,Java,Android,Android Download Manager,活动启动服务以下载n个文件(zip文件)[n从10到60不等,每个文件的大小从100 kb到20 MB不等]发送列表URLSTOBEDOWNLOAD 在服务启动方法中,首先保存一个文件以供下载,一旦下载文件(使用Android的下载管理器),就会有一个广播接收器接收并处理文件以进行解压缩。文件压缩后,将保留另一个文件供下载。代码如下所示: public class DownloadService extends Service { List<String> urlsToBeDown

活动启动服务以下载n个文件(zip文件)[n从10到60不等,每个文件的大小从100 kb到20 MB不等]发送
列表URLSTOBEDOWNLOAD

在服务启动方法中,首先保存一个文件以供下载,一旦下载文件(使用Android的下载管理器),就会有一个广播接收器接收并处理文件以进行解压缩。文件压缩后,将保留另一个文件供下载。代码如下所示:

public class DownloadService extends Service {

List<String> urlsToBeDownloaded;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
   // set urlsToBeDownloaded
   registerReceiver(onComplete, new IntentFilter(
            DownloadManager.ACTION_DOWNLOAD_COMPLETE));
   Utility.downloadFromUrl(urlsToBeDownloaded.get(0),this);
}

BroadcastReceiver onComplete = new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
        new ProcessNext().execute(intent);
    }
};

  ProcessNext extends AsyncTask<Intent, Integer, Integer> {
    @Override
    protected Integer doInBackground(Intent... params) {
        return processNext(params[0]);
    }
 }

 protected int processNext(Intent intent) {
    Query query = new Query();
    ObjectOutputStream oos = null;
        Long dwnId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,
                0);
        query.setFilterById(dwnId);
        DownloadManager manager = (DownloadManager) (this)
                .getSystemService(Context.DOWNLOAD_SERVICE);
        Cursor cursor = manager.query(query);
        if (cursor.moveToFirst()) {
            int status = cursor.getInt(cursor
                    .getColumnIndex(DownloadManager.COLUMN_STATUS));
            String title = cursor.getString(cursor
                    .getColumnIndex(DownloadManager.COLUMN_TITLE));
            File f1 = new File(Environment
                    .getExternalStoragePublicDirectory(
                            Environment.DIRECTORY_DOWNLOADS)
                    .getAbsolutePath()
                    + "/" + title);
            if (status == DownloadManager.STATUS_SUCCESSFUL) {
                 Utility.extractFile(title);
                 urlsToBeDownloaded.remove(0);
                 if(urlsToBeDownloaded.size() != 0)
                     Utility.downloadFromUrl(urlsToBeDownloaded.get(0))
                 else
                     //show done message
            }
           }
          }
     }
公共类下载服务扩展服务{
列表URLStobedownload;
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
//设置URLSTOBEDOWNLOAD
registerReceiver(未完成,新意向过滤器(
DownloadManager.ACTION_DOWNLOAD_COMPLETE);
Utility.downloadFromUrl(urlstobedownload.get(0),this);
}
BroadcastReceiver onComplete=新的BroadcastReceiver(){
公共void onReceive(上下文ctxt,意图){
新建ProcessNext().execute(意图);
}
};
ProcessNext扩展异步任务{
@凌驾
受保护的整数doInBackground(Intent…params){
返回processNext(参数[0]);
}
}
受保护的int processNext(意图){
查询=新查询();
ObjectOutputStream oos=null;
Long dwnId=intent.getLongExtra(DownloadManager.EXTRA\u DOWNLOAD\u ID,
0);
setFilterById(dwnId);
DownloadManager=(DownloadManager)(此)
.getSystemService(Context.DOWNLOAD_服务);
Cursor=manager.query(查询);
if(cursor.moveToFirst()){
int status=cursor.getInt(游标
.getColumnIndex(DownloadManager.COLUMN_状态);
String title=cursor.getString(游标
.getColumnIndex(DownloadManager.COLUMN_TITLE));
文件f1=新文件(环境)
.getExternalStoragePublicDirectory(
环境。目录(下载)
.getAbsolutePath()
+“/”+头衔);
if(status==DownloadManager.status\u SUCCESSFUL){
实用程序文件(标题);
urlstobedownload.remove(0);
如果(urlstobedownload.size()!=0)
Utility.downloadFromUrl(urlstobedownload.get(0))
其他的
//显示完成消息
}
}
}
}
我在这里要做的是按顺序进行,这意味着只有在下载并提取上一个文件的情况下,才会对文件进行下载处理

问题:
当我看到下载文件夹时,一次下载多个文件。我无法理解多个文件是如何下载的。有没有办法解决顺序下载的问题?

为什么同时下载多个文件会有问题?为什么不使用生产者/消费者模型?您是否尝试使用意图service@Williams我试过了,但使用IntentService并没有击中广播接收器。看见