使用Broadcast来获取任务状态

在某些情况下,你可能无法使用Aria的注解,这时你可以选择使用广播来获取Aria的任务状态

使用

首先你需要在配置文件中打开广播接收器

  • 获取任务信息
字段 说明 使用
ISchedulers.ARIA_TASK_INFO_ACTION 广播接收器Action,用于注册广播
ISchedulers.TASK_STATE 获取任务状态 任务状态见下表
ISchedulers.TASK_TYPE 获取任务类型 1: 普通下载任务,2: 普通上传任务,
3: 组合任务, 4: 组合任务的子任务
ISchedulers.TASK_ENTITY 获取任务实体 DownloadEntity, DownloadGroupEntity, UploadEntity
ISchedulers.TASK_SPEED 获取任务速度 long类型,单位为: byte/s,如果需要转换为kb或mb的单位,可自行转换
ISchedulers.TASK_PERCENT 进度百分比 int类型,0-100
taskType = intent.getIntExtra(ISchedulers.TASK_TYPE);
entity = intent.getParcelableExtra(ISchedulers.TASK_ENTITY);

当taskType == 1 或 taskType == 4 时,entity 为DownloadEntity;
当taskType == 2 时,entity 为UploadEntity;
当taskType == 3 是,entity 为DownloadGroupEntity;

  • 任务状态
字段 数值 说明
ISchedulers.PRE 0 任务预加载
ISchedulers.POST_PRE 1 任务预加载完成
ISchedulers.START 2 任务开始
ISchedulers.STOP 3 任务停止
ISchedulers.FAIL 4 任务失败
ISchedulers.CANCEL 5 任务取消
ISchedulers.COMPLETE 6 任务完成
ISchedulers.RUNNING 7 执行中
ISchedulers.RESUME 8 任务恢复
ISchedulers.NO_SUPPORT_BREAK_POINT 9 任务不支持断点
ISchedulers.WAIT 10 任务处于等待中
ISchedulers.SUB_PRE 0xa1 组合认识子任务预处理(组合任务才有)
ISchedulers.SUB_START 0xa2 组合任务子任务开始(组合任务才有)
ISchedulers.SUB_STOP 0xa3 组合任务子任务停止(组合任务才有)
ISchedulers.SUB_CANCEL 0xa4 组合任务子任务取消(组合任务才有)
ISchedulers.SUB_FAIL 0xa5 组合任务子任务失败(组合任务才有)
ISchedulers.SUB_RUNNING 0xa6 组合任务子任务执行执行中(组合任务才有)
ISchedulers.SUB_COMPLETE 0xa7 组合任务子任务完成(组合任务才有)

代码例子

BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override public void onReceive(Context context, Intent intent) {
      if (intent.getAction().equals(ISchedulers.ARIA_TASK_INFO_ACTION)){
        // 获取任务状态 
        ALog.d(TAG, "state = " + intent.getIntExtra(ISchedulers.TASK_STATE, -1)); 
        // 获取任务类型 
        ALog.d(TAG, "type = " + intent.getIntExtra(ISchedulers.TASK_TYPE, -1));
        // 获取任务状态速度,单位为byte/s
        ALog.d(TAG, "speed = " + intent.getLongExtra(ISchedulers.TASK_SPEED, -1));
        // 获取任务进度,0-100
        ALog.d(TAG, "percent = " + intent.getIntExtra(ISchedulers.TASK_PERCENT, -1));
        // 获取任务实体
        ALog.d(TAG, "entity = " + intent.getParcelableExtra(ISchedulers.TASK_ENTITY).toString());
      }
    }
  };

  @Override protected void onResume() {
    super.onResume();
    registerReceiver(receiver, new IntentFilter(ISchedulers.ARIA_TASK_INFO_ACTION));
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(receiver);
    //Aria.download(this).unRegister();
}
Copyright © 2018 laoyuyu. | 蜀ICP备17031160号.            更新时间: 2019-10-24

results matching ""

    No results matching ""