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

问题描述 投票:0回答:1

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

    mFusedLocationClientFast.requestLocationUpdates(mLocationRequest, getPendingIntent());

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

但是,我注意到的一件奇怪的事是,每次IntentService处理handleIntent()中的位置更新时,总是总是在调用同一服务的onStartCommand()之前。

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

android android-pendingintent intentservice location-updates
1个回答
1
投票

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

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

代码为here

[令我感到困惑的是,IntentService的设计人员没有实现onStartCommandonStart final]的实现>

© www.soinside.com 2019 - 2024. All rights reserved.