在Dialog或DialogFragment中使用
例子
public class DownloadDialog extends AbsDialog {
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
@Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop;
@Bind(R.id.cancel) Button mCancel;
@Bind(R.id.size) TextView mSize;
@Bind(R.id.speed) TextView mSpeed;
private static final String DOWNLOAD_URL =
"http://static.gaoshouyou.com/d/4b/d7/e04b308d9cd7f0ad4cac18d1a514544c.apk";
public DownloadDialog(Context context) {
super(context);
init();
}
@Override protected int setLayoutId() {
return R.layout.dialog_download;
}
private void init() {
Aria.download(this).register();
DownloadEntity entity = Aria.download(this).getDownloadEntity(DOWNLOAD_URL);
if (entity != null) {
mSize.setText(CommonUtil.formatFileSize(entity.getFileSize()));
int p = (int) (entity.getCurrentProgress() * 100 / entity.getFileSize());
mPb.setProgress(p);
int state = entity.getState();
setBtState(state != DownloadEntity.STATE_RUNNING);
} else {
setBtState(true);
}
}
@OnClick({ R.id.start, R.id.stop, R.id.cancel }) public void onClick(View view) {
switch (view.getId()) {
case R.id.start:
Aria.download(this)
.load(DOWNLOAD_URL)
.setFilePath(Environment.getExternalStorageDirectory().getPath() + "/飞机大战.apk")
.start();
break;
case R.id.stop:
Aria.download(this).load(DOWNLOAD_URL).stop();
break;
case R.id.cancel:
Aria.download(this).load(DOWNLOAD_URL).cancel();
break;
}
}
@Download.onTaskPre public void onTaskPre(DownloadTask task) {
mSize.setText(CommonUtil.formatFileSize(task.getFileSize()));
setBtState(false);
}
@Download.onTaskStop public void onTaskStop(DownloadTask task) {
setBtState(true);
mSpeed.setText(task.getConvertSpeed());
}
@Download.onTaskCancel public void onTaskCancel(DownloadTask task) {
setBtState(true);
mPb.setProgress(0);
mSpeed.setText(task.getConvertSpeed());
}
@Download.onTaskRunning public void onTaskRunning(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mPb.setProgress(task.getPercent());
mSpeed.setText(task.getConvertSpeed());
}
}
}
注意事项
初始化操作注意事项 从
3.4.3
版本开始,请不要使用Aria.download(getContext())
或Aria.download(getContext())
请使用:
Aria.download(this)
或Aria.download(this)
注册或移除监听注意事项
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override public void onDismiss(DialogInterface dialog) {
}
});
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override public void onCancel(DialogInterface dialog) {
}
});
如上,如果你已经对Dialog对象设置了OnDismissListener
或OnCancelListener
事件,那么Aria将无法跟踪Dialog的生命周期;
这时你需要在Dialog的Dismiss
手动取消Aria的注册
@Override public void dismiss() {
super.dismiss();
Aria.download(this).unRegister();
}