为什么每次服务处理新的Intent时都会调用IntentService的onStartCommand方法

时间:2019-12-24 14:50:33

标签: android android-pendingintent intentservice location-updates

我有一项服务,每1-3秒左右接收一次位置更新。服务是使用未决的意图从主要活动中启动的:

    mFusedLocationClientFast.requestLocationUpdates(mLocationRequest, getPendingIntent());

其中getPendingIntent()是为我的IntentService定义要在handleIntent()中处理的动作的方法

但是,我注意到的一件奇怪的事是,每次IntentService处理handleIntent()中的位置更新时,总是总是紧接着调用相同服务的onStartCommand()。 / p>

有人知道为什么每次服务处理意图时都需要调用onStartCommand()吗? 谢谢

1 个答案:

答案 0 :(得分:1)

IntentService简单来说就是在工作线程上运行onHandleIntent的Service。服务接收意图的机制是onStartCommand

在IntentService中,onStartCommand方法将意图转发到onHandleIntent方法。

代码为here

令我感到困惑的是,IntentService的设计者没有实现onStartCommandonStart final

的实现。