@Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_INIT: // 获得初始化的结果 FileInfo fileInfo = (FileInfo) msg.obj; Log.i("fileTest", fileInfo.toString()); // 启动下载任务 DownloadTask task = new DownloadTask(DownloadService.this, fileInfo, 3); task.download(); // 把下载任务加入 到下载任务集合 mTasks.put(fileInfo.getId(), task); break; } }
@Override public int onStartCommand(Intent intent, int flags, int startId) { if (ACTION_START.equals(intent.getAction())) { FileInfo fileInfo = (FileInfo) intent.getSerializableExtra("fileInfo"); Log.i("fileInfo", "start:" + fileInfo.toString()); // 启动初始化线程 DownloadTask.sExecutorService.execute(new InitThread(fileInfo)); } else if (ACTION_PAUSE.equals(intent.getAction())) { FileInfo fileInfo = (FileInfo) intent.getSerializableExtra("fileInfo"); Log.i("fileInfo", "pause:" + fileInfo.toString()); // 从任务集合获得任务 DownloadTask task = mTasks.get(fileInfo.getId()); // 暂停下载 if (task != null) { task.isPause = true; } } return super.onStartCommand(intent, flags, startId); }