千家信息网

ZlsamDownloadService怎么用

发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,本篇文章为大家展示了ZlsamDownloadService怎么用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。前一阵子,在给公司的智能电视做下载模块的时候发
千家信息网最后更新 2025年02月04日ZlsamDownloadService怎么用

本篇文章为大家展示了ZlsamDownloadService怎么用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

前一阵子,在给公司的智能电视做下载模块的时候发现,android自带的DownloadManager在有线网的情况下没有反应。看了下源码发现标准的手机版android的DownloaderManager就是没有对有线网的情况作处理,于是花了些时间自己写了个下载模块。考虑到国内大多数的智能电视都是基于手机版android改的,所以这里共享一下。

ZlsamDownloadService是一个可以管理多个下载任务的android服务,你把它作为DownloadManager的备选方案。具体功能如下:

  1. 支持多任务

  2. 线程安全

  3. 支持有线网环境

  4. 可以多app共享使用

  5. 持久化状态,下次启动时可以继续未完成任务

  6. 支持插队,对于紧急任务特别有用

最大等待队列:20;

最大处理队列:3;

最大成功队列:20;

最大失败队列:20。

如果以上参数不能满足你的需求,你可以直接在代码中更改,具体位置在TaskQueueManager。

如何使用

如果你想将代码嵌入到你自己的项目当中,你需要将ZlsamDownloadService项目改为library,并在你的项目中引用。在onStart回调中启动和绑定的代码如下:

Intent intent = new Intent("com.zlsam.download.DOWNLOAD_SERVICE"); startService(intent); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); IMainDownloadingService mDownloadService;private ServiceConnection mConnection = new ServiceConnection() {    @Override    public void onServiceConnected(ComponentName name, IBinder service) {        mDownloadService  = IMainDownloadingService.Stub.asInterface(service);        appendLog("Bound to download service.");    }    @Override    public void onServiceDisconnected(ComponentName name) {        mDownloadService = null;    }};

绑定成功之后你可以调用相关方法,这些方法定义在com.zlsam.download.IMainDownloadingService。

下载:

try {    int result = mDownloadService.add2Queue(url, null, null, false);    if (result < 0) {        appendLog("Add task failed, error code: " + result + ", url: " + url);    } else {        appendLog("Add task succeed, url: " + url);    }} catch (RemoteException e) {    e.printStackTrace();}

插队下载:

try {    int result = mDownloadService.add2Queue(url, null, null, true);    if (result < 0) {        appendLog("Add task jump failed, error code: " + result + ", url: " + url);    } else {        appendLog("Add task jump succeed, url: " + url);    }} catch (RemoteException e) {    e.printStackTrace();}

检查任务状态:

try {    state = mDownloadService.queryState(url);} catch (RemoteException e) {    e.printStackTrace();    appendLog("Check task state: exception, url: " + url);}switch (state) {    case -1:        appendLog("Check task state: not found, url: " + url);        break;    case 0:        appendLog("Check task state: waiting, url: " + url);        break;    case 1:        appendLog("Check task state: processing, url: " + url);        break;    case 2:        appendLog("Check task state: succeed, url: " + url);        break;    case 3:        appendLog("Check task state: failed, url: " + url);        break;}

如果你想清除一个已经结束的(成功或者失败)任务(包括已下载的文件):

try {    mDownloadService.clearOne(mFinishedTasks.get(mFinishedTasks.size() - 1));} catch (RemoteException e) {    e.printStackTrace();}

离开界面时你要在onStop回调中解绑服务:

unbindService(mConnection);

调试命令

adb logcat ZlsamDownloadService:V *:S

Demo

你可以直接build安装并启动ZlsamDownloaderService,代码中自带的TestActivity就是一个测试Demo。

上述内容就是ZlsamDownloadService怎么用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

0