日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/1 编程问答 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android服务下载,android服务之bindService和unService中下载任务中的应用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

通過bindService方法來調(diào)用服務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);

}

}

}

});

調(diào)用Service來刪除Down。在ServiceConnection對象的onServiceConnected方法中,執(zhí)行刪除操作之后,調(diào)用unbindService方法來解綁定操作。

/**

* 刪除下載的記錄和內(nèi)容。

*

* @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, "網(wǎng)絡不給力");

return null;

}

if (!SettingSharedPreference.getInstance().getCacheWith2G3G() && !NetUtil.hasWifiNetWork(context)) {

UIUtils.showToast(context, "已經(jīng)禁止2G/3G網(wǎng)絡下載視頻");

return null;

}

synchronized (downList) {

for (Down down : downList) {

// 如果已經(jīng)在下載列表中,直接返回down對象。

if (down.getUrl().equals(video.getMp4Url())) {

UIUtils.showToast(context, "已經(jīng)在下載列表中");

return down;

}

}

}

// 如果是一個新的下載,發(fā)送消息開始一個新的下載。

Message.obtain(handler, DOWN_URL, video).sendToTarget();

return null;

}

@Override

public List 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方法中創(chuàng)建DownBinder實例。downBinder = new DownBinder();

在onBind方法中返回DownBinder實例。

@Override

public IBinder onBind(Intent intent) {

return downBinder;

}

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的android服务下载,android服务之bindService和unService中下载任务中的应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。