android服务之bindService和unService中下载任务中的应用

通过bindService方法来调用服务

final Down down = data;
viewHolder.videoActionBtn.setOnClickListener(new View.OnClickListener() {
                                                                                       
    @Override
    public void onClick(View v) {
        if(bEditMode) {
            // 处于编辑模式,点击删除
            deleteDown(down);
        } else {
            // 处于播放模式,点击播放
            VideoItemData video = (new Gson()).fromJson(down.getData(),
                    VideoItemData.class);
            if(video != null) {
                video.setMp4Url(down.getSavaPath());
                VideoUtils.playVideo(context, video);
            }
        }
    }
});

调用Service来删除Down。在ServiceConnection对象的onServiceConnected方法中,执行删除操作之后,调用unbindService方法来解绑定操作。

/**
 * 删除下载的记录和内容。
 *
 * @param down
 */
private void deleteDown(final Down down){
    Intent intent = new Intent(context, DownService.class);
    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            ((DownBinder)service).deleteDown(down, true);
            context.unbindService(this);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
                                                                                 
        }
    };
    context.bindService(intent, conn, Context.BIND_AUTO_CREATE);
}

在Service类中定义Binder的子类。

public class DownBinder extends Binder implements IDown {
    @Override
    public Down downVideo(VideoItemData video) {
        if (!NetUtil.hasNetwork(context)) {
            UIUtils.showToast(context, "网络不给力");
            return null;
        }
        if (!SettingSharedPreference.getInstance().getCacheWith2G3G() && !NetUtil.hasWifiNetWork(context)) {
            UIUtils.showToast(context, "已经禁止2G/3G网络下载视频");
            return null;
        }
        synchronized (downList) {
            for (Down down : downList) {
                // 如果已经在下载列表中,直接返回down对象。
                if (down.getUrl().equals(video.getMp4Url())) {
                    UIUtils.showToast(context, "<" + video.getTitle() + ">已经在下载列表中");
                    return down;
                }
            }
        }
        // 如果是一个新的下载,发送消息开始一个新的下载。
        Message.obtain(handler, DOWN_URL, video).sendToTarget();
        return null;
    }
    @Override
    public List<Down> getAllDown() {
        return getDownList();
    }
    @Override
    public void pauseDown(Down down) {
        downHandler.removeMessages(START_DOWN);
        down = getExistDown(down);
        downHandler.sendMessage(downHandler.obtainMessage(PAUSE_DOWN, down));
    }
    @Override
    public void startDown(Down down) {
        downHandler.removeMessages(PAUSE_DOWN);
        down = getExistDown(down);
        downHandler.sendMessage(downHandler.obtainMessage(START_DOWN, down));
    }
    /**
     * 取出对应的Down 从downList中取出的Down都赋值了DownTask
     *
     * @param down
     * @return
     */
    private Down getExistDown(Down down) {
        for (Down d : downList) {
            if (down.getUrl().equals(d.getUrl())) {
                return d;
            }
        }
        return down;
    }
    @Override
    public void deleteDown(Down down, boolean deleteFile) {
        downHandler.removeMessages(START_DOWN);
        downHandler.removeMessages(PAUSE_DOWN);
        down = getExistDown(down);
        downHandler.obtainMessage(DELETE_DOWN, (deleteFile ? 1 : 0), 0, down).sendToTarget();
    }
    @Override
    public void clearDown(boolean deleteFile) {
        downHandler.removeMessages(START_DOWN);
        downHandler.removeMessages(PAUSE_DOWN);
        downHandler.obtainMessage(CLEAR_DOWN, deleteFile).sendToTarget();
    }
    @Override
    public void reStartDown(Down down) {
        down = getExistDown(down);
        downHandler.obtainMessage(RESTART_DOWN, down).sendToTarget();
    }
}

在onCreate方法中创建DownBinder实例。

downBinder = new DownBinder();

在onBind方法中返回DownBinder实例。

@Override
public IBinder onBind(Intent intent) {
    return downBinder;
}


你可能感兴趣的:(bindService,unbindService,下载任务中应用)