图片加载框架NotificationChannelNetty

Aria文件下载

2020-11-24  本文已影响0人  陈陈_04d0

首先 gradle 添加依赖 注册

 //文件下载

implementation'com.arialyy.aria:core:3.8.7'

annotationProcessor'com.arialyy.aria:compiler:3.8.7'

Aria.download(this).register();



服务器数据请求版本好

int code=接口获取;

entity = Aria.download(this).getFirstDownloadEntity(url);//单个任务下载

if (entity !=null) {//已经有下载记录

    if (code > BaseUtil.getVersionCode(this)) {

        mTaskId =entity.getId();

        tv_downing.setText(getString(R.string.string_down_progress, CommonUtil.formatFileSize(entity.getCurrentProgress()), CommonUtil.formatFileSize(entity.getFileSize())));//进度

        int p = (int) (entity.getCurrentProgress() *100 /entity.getFileSize());

        progressBar.setProgress(p);//进度条

        tv_complete_loading.setEnabled(entity.getState() != DownloadEntity.STATE_RUNNING);

        setLoadState();//状态设置

        if (entity.getState() == DownloadEntity.STATE_COMPLETE) {//如果下载完成了,那么安装

          InstallApkUtil.Install(this, entity.getFilePath());

            rl_progressBar.setVisibility(View.GONE);

        }

}else {

     //新版本已经安装删除文件记录

        Aria.download(this).load(mTaskId).cancel(true);

    }

}else {

//提示下载弹窗

    tv_complete_loading.setEnabled(true);

}


设置按钮状态跟点击

case R.id.tv_complete_loading:

if (mTaskId == -1) {

mTaskId = Aria.download(this)

.load(url)

.setFilePath(getFilePath(this, "MVP", System.currentTimeMillis() +"_" +code +".apk").getAbsolutePath())

.create();

        tv_complete_loading.setText(getString(R.string.stop));

break;

    }

if (Aria.download(this).load(mTaskId).isRunning()) {

Aria.download(this).load(mTaskId).stop();

        tv_speed.setText(getString(R.string.down_speed_normal));

        tv_complete_loading.setText(getString(R.string.recover));

    }else {

Aria.download(this).load(mTaskId).resume();

        tv_complete_loading.setText(getString(R.string.stop));

    }

break;


状态判断

public void setLoadState() {

String btStr ="";

    String stateStr ="";

    switch (entity.getState()) {

case IEntity.STATE_WAIT:

btStr = getResources().getString(R.string.start);

            stateStr = getResources().getString(R.string.waiting);

break;

        case IEntity.STATE_OTHER:

case IEntity.STATE_FAIL:

btStr = getResources().getString(R.string.start);

            stateStr = getResources().getString(R.string.load_fail_hint);

break;

        case IEntity.STATE_STOP:

btStr = getResources().getString(R.string.recover);

            stateStr = getResources().getString(R.string.stop);

break;

        case IEntity.STATE_PRE:

case IEntity.STATE_POST_PRE:

case IEntity.STATE_RUNNING:

btStr = getResources().getString(R.string.stop);

            stateStr =entity.getConvertSpeed();

break;

        case IEntity.STATE_COMPLETE:

btStr = getResources().getString(R.string.re_start);

            stateStr = getResources().getString(R.string.completed);

break;

        case IEntity.STATE_CANCEL:

btStr = getResources().getString(R.string.close);

break;

        default:

btStr = getResources().getString(R.string.start);

            stateStr ="";

break;

    }

tv_complete_loading.setText(btStr);

}


//在这里处理任务执行中的状态,如进度进度条的刷新

@Download.onTaskRunning

protected void running(DownloadTask task) {

//任务进度百分比

    progressBar.setProgress(task.getPercent());

    //转换单位后的下载速度,单位转换需要在配置文件中打开

    tv_speed.setText(task.getConvertSpeed());

    tv_downing.setText(getString(R.string.string_down_progress, task.getConvertCurrentProgress(), task.getConvertFileSize()));

    Log.e("DownloadTask1", task.getConvertCurrentProgress() +"==" + task.getConvertFileSize() +"==" + task.getPercent() +"==" + task.getConvertSpeed());

}

@Download.onTaskComplete

void taskComplete(DownloadTask task) {

//在这里处理任务完成的状态

    Log.e("DownloadTask2", task.getFilePath() +"==" + task.getConvertCurrentProgress() +"==" + task.getConvertFileSize());

    InstallApkUtil.Install(this, task.getFilePath());

    rl_progressBar.setVisibility(View.GONE);

}


xml布局

    android:id="@+id/rl_progressBar"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_margin="@dimen/x13">

        android:id="@+id/tv_speed"

        style="@style/Gray9Text11"

        android:layout_marginBottom="@dimen/x10"

        android:text="@string/down_speed_normal" />

        android:id="@+id/tv_downing"

        style="@style/Gray9Text11"

        android:layout_alignParentEnd="true"

        android:text="0/0mb" />

        android:id="@+id/progressBar"

        style="?android:attr/progressBarStyleHorizontal"

        android:layout_width="wrap_content"

        android:layout_height="20dp"

        android:layout_alignTop="@id/tv_complete_loading"

        android:layout_toStartOf="@id/tv_complete_loading"

        android:max="100" />

        android:id="@+id/tv_complete_loading"

        style="@style/BlueText11"

        android:layout_below="@id/tv_speed"

        android:layout_alignParentEnd="true"

        android:layout_marginStart="@dimen/x10"

        android:gravity="center"

        android:text="@string/start" />

上一篇下一篇

猜你喜欢

热点阅读