其他分享
首页 > 其他分享> > DownloadServiceAbility

DownloadServiceAbility

作者:互联网

public class DownloadServiceAbility extends Ability implements IDownloadListener {
    private static final String TAG = "idmxm";

    private List<DownloadSession> downloadSessionList = new ArrayList<>();

    private class DownloadRemoteObject extends RemoteObject {
        private DownloadRemoteObject() {
            super("Remote");
        }

        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
            switch (code) {
                case Const.REMOTE_REQUEST_CODE_NEW_TASK: {
                    startDownload(data.readString());
                    reply.writeInt(Const.SEND_REQUEST_SUCCESS);
                    break;
                }
                case Const.REMOTE_REQUEST_CODE_CANCEL_TASK: {
                    cancelDownload();
                    reply.writeInt(Const.SEND_REQUEST_SUCCESS);
                    break;
                }
                default:
                    break;
            }
            return true;
        }
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }

    @Override
    protected IRemoteObject onConnect(Intent intent) {
        return new DownloadRemoteObject();
    }

    @Override
    protected void onStop() {
        LogUtil.info(TAG, "service on stop");
        for (DownloadSession downloadSession : downloadSessionList) {
            downloadSession.remove();
            downloadSession.removeListener(this);
        }
        super.onStop();
    }

    private void startDownload(String url) {
        Uri uri = Uri.parse(url);
        DownloadConfig config = new DownloadConfig.Builder(this, uri).setPath(null, "BackgroundDownload")
            .setTitle(url)
            .setDescription("This is a download session")
            .setNetworkRestriction(DownloadConfig.NETWORK_WIFI | DownloadConfig.NETWORK_MOBILE)
            .build();
        DownloadSession downloadSession = new DownloadSession(this, config);
        downloadSession.start();
        downloadSession.addListener(this);
        downloadSessionList.add(downloadSession);
        sendMessage("start download");
        LogUtil.info(TAG, "start download: " + url);
    }

    private void cancelDownload() {
        LogUtil.info(TAG, "cancel download: --");
        int index = downloadSessionList.size() - 1;
        if (index < 0) {
            return;
        }
        downloadSessionList.get(index).remove();
        downloadSessionList.get(index).removeListener(this);
        downloadSessionList.remove(index);
        LogUtil.info(TAG, "cancel download: " + index);
    }

    private void sendMessage(String message) {
        InnerEvent innerEvent = InnerEvent.get(Const.HANDLER_EVENT_ID, Const.HANDLER_EVENT_PARAM, message);
        getEventHandler().sendEvent(innerEvent);
    }

    @Override
    public void onRemoved() {
        LogUtil.info(TAG, "session on removed");
        sendMessage("session removed");
    }

    @Override
    public void onCompleted() {
        LogUtil.info(TAG, "onCompleted");
        sendMessage("session complete");
    }

    @Override
    public void onFailed(int errorCode) {
        LogUtil.info(TAG, "onFailed: " + errorCode);
        sendMessage("session fail");
    }

    @Override
    public void onProgress(long receivedSize, long totalSize) {
        LogUtil.info(TAG, "progress: " + receivedSize + " / " + totalSize);
        sendMessage(receivedSize + " / " + totalSize);
    }
}

 

标签:info,DownloadServiceAbility,void,TAG,LogUtil,Override,downloadSessionList
来源: https://www.cnblogs.com/tel13207749644/p/15041768.html