一.intenService是什么
IntenService是继承并处理异步线程的一个类,在IntenService里面有一个工作线程来处理耗时任务,启动IntenService和启动传统的Service一样,同时,当任务完成后,IntenService会自动停止,而不需要我们手动去控制或者stop()。另外,IntenService可以启动多次,而每一个耗时操作会以工作队列的方式在IntenService的onHandleIntent回调方法里执行。并且,每次只能执行一个工作线程,执行完第一个再执行第二个
二.intenService使用
IntentServiceActivity
public class IntentServiceActivity extends AppCompatActivity implements MyIntentService.UpdateUI {
/**
* 图片地址集合
*/
private String url[] = {
"http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg",
"http://sucai.qqjay.com/qqjayxiaowo/201210/26/1.jpg",
"http://img02.tooopen.com/images/20140504/sy_60294738471.jpg",
"http://img2.imgtn.bdimg.com/it/u=819201812,3553302270&fm=214&gp=0.jpg",
"http://pic55.nipic.com/file/20141208/19462408_171130083000_2.jpg",
"http://img05.tooopen.com/images/20150531/tooopen_sy_127457023651.jpg",
"http://pic.58pic.com/58pic/17/41/38/88658PICNuP_1024.jpg"
};
private ImageView imageView;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
imageView.setImageBitmap((Bitmap) msg.obj);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent_service);
imageView = findViewById(R.id.imageview);
Intent intent = new Intent(this, MyIntentService.class);
for (int i = 0; i < 7; i++) {
intent.putExtra(MyIntentService.DOWNLOAD_URL, url[i]);
intent.putExtra(MyIntentService.INDEX_FLAG, i);
startService(intent);
}
MyIntentService.setUpdateUi(this);
}
@Override
public void updateUi(Message message) {
mHandler.sendMessageDelayed(message, message.what * 1000);
}
}
MyIntentService
public class MyIntentService extends IntentService {
public static final String DOWNLOAD_URL = "download_url";
public static final String INDEX_FLAG = "index_flag";
public static UpdateUI updateUI;
public MyIntentService() {
super("service");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Bitmap bitmap = downloadUrlBitmap(intent.getStringExtra(DOWNLOAD_URL));
Message message = new Message();
message.what = intent.getIntExtra(INDEX_FLAG, 0);
message.obj = bitmap;
if (updateUI != null) {
updateUI.updateUi(message);
}
}
public static void setUpdateUi(UpdateUI updateUi) {
updateUI = updateUi;
}
private Bitmap downloadUrlBitmap(String urlString) {
HttpURLConnection urlConnection = null;
BufferedInputStream in = null;
Bitmap bitmap = null;
try {
final URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024);
bitmap = BitmapFactory.decodeStream(in);
} catch (final IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
try {
if (in != null) {
in.close();
}
} catch (final IOException e) {
e.printStackTrace();
}
}
return bitmap;
}
public interface UpdateUI {
void updateUi(Message message);
}
}
三.intenService源码分析
intenService源码也不是很多,先看看oncreate方法
@Override
public void onCreate() {
// TODO: It would be nice to have an option to hold a partial wakelock
// during processing, and to have a static startService(Context, Intent)
// method that would launch the service & hand off a wakelock.
super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
先创建一个HandlerThread ,说明IntentService内部是利用HandlerThread进行消息传递的,然后创建了一个Handler(ServiceHandler),上一步创建了一个Looper,这个Looper是什么?Looper对象来自HandlerThread,ServiceHandler就变成了处理异步线程的执行类了。
@Override
public void onStart(@Nullable Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);
}
}
主动调用 stopSelf(msg.arg1)这个方法,和stopSelf()方法不一样,没有参数会立即停止,等待所有消息处理完之后,终止任务。
protected abstract void onHandleIntent(@Nullable Intent intent);
网友评论