前言
IntentService的使用,定义的自定义的类继承IntentService,重写onHandleIntent()方法加入自己的业务逻辑即可,该方法是在工作线程中运行的……
使用方式
1、继承IntentService
class MyService extends IntentService {
}
2、重写onHandleIntent()方法,在里面加入你自己的业务逻辑
3、
3、在其他组件中,使用startService()方法启动Service
startService(传入Intent对象)
onHandleIntent()方法分析
protected abstract void onHandleIntent(@Nullable Intent intent);
根据不同的业务,传入不同状态的Intent对象,在该方法内部通过Intent对象区分要进行的工作
onStartCommand()方法分析
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
onStart(intent, startId);
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}
startService()调用后,onStartCommand()方法会被回调
第一个参数Intent对象:在startService()方法中传入的Intent对象
第二个参数flags则代表:Additional data about this start request.
第三个参数startId则代表:A unique integer representing this specific request to start. Use with {@link #stopSelfResult(int)},startId则是stopSelfResult()方法就用到的,官方也说明你去看stopSelf方法就对了
onStart()方法的调用,会涉及到onHandleIntent()方法的调用,这里先不展开了……