一、概述
我们都知道在Service里面我们不能直接进行耗时操作,一般都需要去开启子线程去做一些事情,自己去管理Service的生命周期以及子线程并非是个优雅的做法;好在Android给我们提供了一个类,叫做IntentService。
IntentService是一个基于Service的一个类,用来处理异步的请求。可以通过startService(Intent)来提交请求,该Service会在需要的时候创建,当完成所有的任务以后自己关闭,且请求是在工作线程处理的。
IntentService负责执行后台的耗时的任务,也因为IntentService是服务的原因,这导致它的优先级比单纯的线程要高。
所以IntentService适用于执行高优先级的后台任务。
使用IntentService最起码有两个好处,一方面不需要自己去new Thread了;另一方面不需要考虑在什么时候关闭该Service。
二、代码示例
package com.aldx.presidentcockpit.service;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import com.aldx.presidentcockpit.model.MonitorInfo;
import com.aldx.presidentcockpit.utils.LogUtil;
import com.aldx.presidentcockpit.utils.Utils;
import com.hikvision.netsdk.HCNetSDK;
import com.hikvision.netsdk.INT_PTR;
import com.hikvision.netsdk.NET_DVR_JPEGPARA;
/**
* author: chenzheng
* created on: 2018/4/24 14:59
* description:
*/
public class DownloadImgService extends IntentService {
public static final String DOWNLOAD_RESULT = "com.aldx.presidentcockpit.service.DOWNLOAD_RESULT";
private static final String ACTION_DOWNLOAD_IMG = "com.aldx.presidentcockpit.service.action.DOWNLOAD_IMAGE";
public static void startDownloadImg(Context context,int loginId, int channel, String path) {
Intent intent = new Intent(context, DownloadImgService.class);
intent.setAction(ACTION_DOWNLOAD_IMG);
intent.putExtra("hk_login_id", loginId);
intent.putExtra("hk_channel", channel);
intent.putExtra("hk_image_path", path);
context.startService(intent);
}
public DownloadImgService() {
super("DownloadImgService");
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_DOWNLOAD_IMG.equals(action)) {
String path = intent.getStringExtra("hk_image_path");
int loginId = intent.getIntExtra("hk_login_id", -1);
int channel = intent.getIntExtra("hk_channel", -1);
handleUploadImg(loginId, channel, path);
}
}
}
private void handleUploadImg(int loginId, int channel, String path) {
try {
NET_DVR_JPEGPARA jpeg = new NET_DVR_JPEGPARA();
// 设置图片的分辨率
jpeg.wPicSize = 2;
// 设置图片质量
jpeg.wPicQuality = 2;
boolean is = HCNetSDK.getInstance().NET_DVR_CaptureJPEGPicture(loginId,
channel, jpeg, path);
if (is) {
LogUtil.e("成功:" + path);
} else {
LogUtil.e("失败:" + path);
LogUtil.e(HCNetSDK.getInstance().NET_DVR_GetErrorMsg(new INT_PTR()));
}
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
Intent intent = new Intent(DOWNLOAD_RESULT);
localBroadcastManager.sendBroadcast(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onCreate() {
super.onCreate();
LogUtil.e("TAG", "onCreate");
}
@Override
public void onDestroy() {
super.onDestroy();
LogUtil.e("TAG", "onDestroy");
}
}
public static final String UPLOAD_RESULT = "com.aldx.presidentcockpit.service.DOWNLOAD_RESULT";
//注册广播接收器
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
IntentFilter intentFilter = new IntentFilter(UPLOAD_RESULT);
localBroadcastManager.registerReceiver(downloadHkImgReceiver, intentFilter);
private BroadcastReceiver downloadHkImgReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
LogUtil.e("接收图片下载结果广播");
if (mWorkering && intent.getAction().equals(UPLOAD_RESULT)) {
videoRecyclerview.refreshComplete();
if (playMonitorList.size() > 0) {
loadingLayout.showContent();
} else {
loadingLayout.showEmpty();
}
adapter.setItems(playMonitorList);
}
}
};
for (int i = 0; i < playMonitorList.size(); i++) {
MonitorInfo monitorInfo = playMonitorList.get(i);
String imgPath = Utils.getHikvisionCapturePath(getActivity()) + "/" + System.currentTimeMillis() + ".jpg";
monitorInfo.imagePath = imgPath;
DownloadImgService.startDownloadImg(getActivity(), m_iLogID, monitorInfo.chanl, imgPath);
}
@Override
public void onDestroy() {
super.onDestroy();
mWorkering = false;
if (downloadHkImgReceiver != null) {
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(downloadHkImgReceiver);
downloadHkImgReceiver = null;
}
}
<service android:name="com.aldx.presidentcockpit.service.DownloadImgService" />
当Service的每个任务完成的时候,会发送一个广播,我们在Activity/Fragment里面分别注册和解注册了广播;当收到广播则更新指定的UI。