gpt4 book ai didi

android - 在 Android 中运行多个并行下载

转载 作者:行者123 更新时间:2023-11-30 01:53:58 27 4
gpt4 key购买 nike

首先,我扩展了 IntentService 来构建我的 DonwloadService 类。没问题,但是已经创建了一个下载队列,第二次下载必须等待第一次下载完成等等。

所以我决定扩展 Service 类(在 docs 之后)。我刚刚添加了下载功能和发布结果的功能( Activity 中接收者收听):

    public class DownloadService extends Service
{
private static final String TAG = DownloadService.class.getSimpleName();

public static final String EXTRA_DOWNLOAD = "EXTRA_DOWNLOAD";
public static final String EXTRA_POSITION = "EXTRA_POSITION";
public static final String INTENT_NOTIFICATION = "INTENT_NOTIFICATION";
public static final String EXTRA_RESULT = "EXTRA_RESULT";
public static final String EXTRA_PROGRESS = "EXTRA_PROGRESS";
public static final String EXTRA_PATH = "EXTRA_PATH";
public static final String EXTRA_INDETERMINABLE = "EXTRA_INDETERMINABLE";
public static final int RESULT_PROGRESS = 123;

private int mResult = Activity.RESULT_CANCELED;

private Looper mServiceLooper;
private ServiceHandler mServiceHandler;

private final class ServiceHandler extends Handler
{
public ServiceHandler(Looper looper)
{
super(looper);
}

@Override
public void handleMessage(Message message)
{
// Download file
download(message.getData());

// Stop the service using the startId, so that we don't stop
// the service in the middle of handling another job
stopSelf(message.arg1);
}
}

@Override
public void onCreate()
{
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work will not disrupt our UI.
HandlerThread handlerThread = new HandlerThread("DownloadServiceStartArguments",
Process.THREAD_PRIORITY_BACKGROUND);
handlerThread.start();

// Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = handlerThread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
// For each start request, send a message to start a job and deliver the
// start ID so we know which request we're stopping when we finish the job
Message message = mServiceHandler.obtainMessage();
message.setData(intent.getExtras());
message.arg1 = startId;
mServiceHandler.sendMessage(message);

return START_REDELIVER_INTENT;
}

@Nullable
@Override
public IBinder onBind(Intent intent)
{
// No binding provided
return null;
}

@Override
public void onDestroy()
{
Log.d(TAG, "Service done");
}

/**
* Handles file download
*
* @param bundle
*/
private void download(Bundle bundle)
{
if (bundle != null) {
return;
}

Download download = bundle.getParcelable(EXTRA_DOWNLOAD);
int position = bundle.getInt(EXTRA_POSITION, -1);

File downloadedFile = new File(Environment.getExternalStorageDirectory(), position + ".jpg");
if (downloadedFile.exists()) {
downloadedFile.delete();
}

FileOutputStream fileOutputStream = null;

int filesize = -1;

try {

fileOutputStream = new FileOutputStream(downloadedFile.getPath());

URL url = new URL(download.getUrl());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();

filesize = connection.getContentLength();

InputStream reader = new BufferedInputStream(connection.getInputStream());

byte data[] = new byte[1024];

int next = -1;
int total = 0;
while ((next = reader.read(data)) != -1) {
mResult = RESULT_PROGRESS;
total += next;
publishResult(downloadedFile.getAbsolutePath(),
(filesize > 0) ? Math.round(total * 100 / filesize) : Math.round(total / 1024),
position, filesize <= 0);

fileOutputStream.write(data, 0, next);
}

mResult = Activity.RESULT_OK;

} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
publishResult(downloadedFile.getAbsolutePath(), 100, position, filesize <= 0);

}

private void publishResult(String downloadPath, int progress, int positionInList,
boolean indeterminable)
{
Intent intent = new Intent(INTENT_NOTIFICATION);
intent.putExtra(EXTRA_PATH, downloadPath);
intent.putExtra(EXTRA_PROGRESS, progress);
intent.putExtra(EXTRA_POSITION, positionInList);
intent.putExtra(EXTRA_INDETERMINABLE, indeterminable);
intent.putExtra(EXTRA_RESULT, mResult);
sendBroadcast(intent);
}
}

但是还是有下载队列,没有并行下载。

最佳答案

如果您想在不同的数据集上重复运行一个任务,但您一次只需要执行一次,IntentService 可以满足您的需求。要在资源可用时自动运行任务,或允许多个任务同时运行(或两者同时运行),您需要提供一个托管的线程集合。为此,请使用 ThreadPoolExecutor 实例,当池中的线程空闲时,它会从队列中运行任务。要运行任务,您只需将其添加到队列中即可。

引用: Creating a Manager for Multiple Threads

关于android - 在 Android 中运行多个并行下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32559183/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com