Location服务是系统中很重要的一个服务,几乎当前所有的App都会用到这个服务。

首先看代码在Android源码的位置

Android API

frameworks/base/location

LocationManagerService

frameworks/base/services/core/java/com/android/server/location
frameworks/base/services/core/java/com/android/server/LocationManagerService.java

SystemServer

作为一个系统级别服务,启动肯定是在SystemServer中进行。

代码入口:frameworks/base/services/java/com/android/server/SystemServer.java

启动LocationManagerService的代码在startOtherServices()函数里面

执行流程如下

  1. 创建LocationManagerService对象
  2. 将创建好的的对象加入到ServiceManager里面
  3. 调用LocationManagerServicesystemRunning()函数初始化相关的Provider
private void startOtherServices() {
////////////////////
LocationManagerService location = null;
traceBeginAndSlog("StartLocationManagerService");
try {
location = new LocationManagerService(context);
ServiceManager.addService(Context.LOCATION_SERVICE, location);
} catch (Throwable e) {
reportWtf("starting Location Manager", e);
}
traceEnd();
// These are needed to propagate to the runnable below.
final LocationManagerService locationF = location;
traceBeginAndSlog("MakeLocationServiceReady");
try {
if (locationF != null) locationF.systemRunning();
} catch (Throwable e) {
reportWtf("Notifying Location Service running", e);
}
traceEnd();
////////////////////
}

LocationManagerService

从ServiceManager启动后,会调用systemRunning()方法,这个函数里面会初始化所有的可用的Providers

代码路径:frameworks/base/services/core/java/com/android/server/LocationManagerService.java

构造函数

构造函数很简单,除了初始化变量外,就给PackageManager设置了一个PackageProvider

public LocationManagerService(Context context) {
super();
mContext = context;
mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
// Let the package manager query which are the default location
// providers as they get certain permissions granted by default.
PackageManagerInternal packageManagerInternal = LocalServices.getService(
PackageManagerInternal.class);
packageManagerInternal.setLocationPackagesProvider(
new PackageManagerInternal.PackagesProvider() {
@Override
public String[] getPackages(int userId) {
return mContext.getResources().getStringArray(
com.android.internal.R.array.config_locationProviderPackageNames);
}
});
if (D) Log.d(TAG, "Constructed");
// most startup is deferred until systemRunning()
}

systemRunning()

作为LocationManagerService的入口函数,主要做以下几部分工作

  1. 初始化成员变量,主要是个各种需要用的Manager
  2. 初始化Provider
  3. 注册Observer,主要是包括Settings,UserChange等
public void systemRunning() {
synchronized (mLock) {
if (D) Log.d(TAG, "systemRunning()");
// prepare providers
loadProvidersLocked();
updateProvidersLocked();
}
// listen for settings changes
// ..... //
// listen for user change
// ..... //
}

我们主要看第二点,包括了各种Provider的初始化

loadProvidersLocked

加载顺序如下

1. PassiveProvider,必须
2. GnssLocationProvider,如果有,则加载
3. FuseProvider检查,这里比较重要,如果通不过,会跑出异常,不会进行下面的步骤(systemRunning会结束)
4. Bind NetworkProvider,第三方Provider
5. Bind Fused provider
6. Bind Geocoder provider
7. Bind Geofence provider

另外,针对每一个Provider,创建的步骤大致都是以下几个步骤

{
XxxProvider xxxProvider = new XxxProvider(this);
addProviderLocked(xxxProvider);
mEnabledProviders.add(xxxProvider.getName());
// or
mRealProviders.put(LocationManager.Xxx_PROVIDER, xxxProvider);
mXxxProvider = xxxProvider;
}

部分Provider初始化相对复杂一点,但是整体的逻辑一致。我们这里重点分析GnssProvider

private void loadProvidersLocked() {
// create a passive location provider, which is always enabled
PassiveProvider passiveProvider = new PassiveProvider(this);
addProviderLocked(passiveProvider);
mEnabledProviders.add(passiveProvider.getName());
mPassiveProvider = passiveProvider; if (GnssLocationProvider.isSupported()) {
// 创建GnssProvider(老版本的Android上是GPS,后面整合成了GNSS,支持不同类型的导航系统)
GnssLocationProvider gnssProvider = new GnssLocationProvider(mContext, this,
mLocationHandler.getLooper());
mGnssSystemInfoProvider = gnssProvider.getGnssSystemInfoProvider();
mGnssBatchingProvider = gnssProvider.getGnssBatchingProvider();
mGnssMetricsProvider = gnssProvider.getGnssMetricsProvider();
mGnssStatusProvider = gnssProvider.getGnssStatusProvider();
mNetInitiatedListener = gnssProvider.getNetInitiatedListener();
addProviderLocked(gnssProvider);
mRealProviders.put(LocationManager.GPS_PROVIDER, gnssProvider);
mGnssMeasurementsProvider = gnssProvider.getGnssMeasurementsProvider();
mGnssNavigationMessageProvider = gnssProvider.getGnssNavigationMessageProvider();
mGpsGeofenceProxy = gnssProvider.getGpsGeofenceProxy();
} /*
Load package name(s) containing location provider support.
These packages can contain services implementing location providers:
Geocoder Provider, Network Location Provider, and
Fused Location Provider. They will each be searched for
service components implementing these providers.
The location framework also has support for installation
of new location providers at run-time. The new package does not
have to be explicitly listed here, however it must have a signature
that matches the signature of at least one package on this list.
*/
Resources resources = mContext.getResources();
ArrayList<String> providerPackageNames = new ArrayList<>();
String[] pkgs = resources.getStringArray(
com.android.internal.R.array.config_locationProviderPackageNames);
if (D) {
Log.d(TAG, "certificates for location providers pulled from: " +
Arrays.toString(pkgs));
}
if (pkgs != null) providerPackageNames.addAll(Arrays.asList(pkgs)); // 注意这里如果没有找到FuseProvider,会抛出异常,结束这个初始化流程
ensureFallbackFusedProviderPresentLocked(providerPackageNames); // bind to network provider
// bind to fused provider
// bind to geocoder provider
// bind to geofence provider
// bind to hardware activity recognition
}

updateProvidersLocked

上一个步骤仅仅只是进行了Provider的初始化操作,这里会调用Provider的enable方法,启用相关的Provider,主要调用

updateProviderListenersLocked(String provider, boolean enabled)

private void updateProvidersLocked() {
boolean changesMade = false;
// 遍历所有的Provider
for (int i = mProviders.size() - 1; i >= 0; i--) {
LocationProviderInterface p = mProviders.get(i);
boolean isEnabled = p.isEnabled();
String name = p.getName();
// 通过当前用户的Settings来判断是否需要启动这个Provider
boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name);
if (isEnabled && !shouldBeEnabled) {
// 如果已经启动并且是不需要被启动,就会关闭
// 并且清除之前的记录
updateProviderListenersLocked(name, false);
// If any provider has been disabled, clear all last locations for all providers.
// This is to be on the safe side in case a provider has location derived from
// this disabled provider.
mLastLocation.clear();
mLastLocationCoarseInterval.clear();
changesMade = true;
} else if (!isEnabled && shouldBeEnabled) {
// 如果没有启动,并且需要被启动,则启动
updateProviderListenersLocked(name, true);
changesMade = true;
}
}
// 如果状态改变,就发系统广播通知
if (changesMade) {
mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
UserHandle.ALL);
mContext.sendBroadcastAsUser(new Intent(LocationManager.MODE_CHANGED_ACTION),
UserHandle.ALL);
}
}

updateProviderListenersLocked

private void updateProviderListenersLocked(String provider, boolean enabled) {
int listeners = 0;
LocationProviderInterface p = mProvidersByName.get(provider);
if (p == null) return;
ArrayList<Receiver> deadReceivers = null;
ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
if (records != null) {
for (UpdateRecord record : records) {
if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mIdentity.mUid))) {
// Sends a notification message to the receiver
if (!record.mReceiver.callProviderEnabledLocked(provider, enabled)) {
if (deadReceivers == null) {
deadReceivers = new ArrayList<>();
}
deadReceivers.add(record.mReceiver);
}
listeners++;
}
}
}
if (deadReceivers != null) {
for (int i = deadReceivers.size() - 1; i >= 0; i--) {
removeUpdatesLocked(deadReceivers.get(i));
}
}
// 根据传入的参数,确定是开启还是关闭相关的Provider
if (enabled) {
p.enable();
if (listeners > 0) {
// 如果存在Locatoin listener,会调用下面的方法
applyRequirementsLocked(provider);
}
} else {
p.disable();
}
}

applyRequirementsLocked

这里会根据请求的Record的参数,来向具体的Provider发起位置信息请求。

private void applyRequirementsLocked(String provider) {
LocationProviderInterface p = mProvidersByName.get(provider);
if (p == null) return;
ArrayList<UpdateRecord> records = mRecordsByProvider.get(provider);
WorkSource worksource = new WorkSource();
ProviderRequest providerRequest = new ProviderRequest();
ContentResolver resolver = mContext.getContentResolver();
long backgroundThrottleInterval = Settings.Global.getLong(
resolver,
Settings.Global.LOCATION_BACKGROUND_THROTTLE_INTERVAL_MS,
DEFAULT_BACKGROUND_THROTTLE_INTERVAL_MS);
// initialize the low power mode to true and set to false if any of the records requires
providerRequest.lowPowerMode = true;
if (records != null) {
for (UpdateRecord record : records) {
if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mIdentity.mUid))) {
if (checkLocationAccess(
record.mReceiver.mIdentity.mPid,
record.mReceiver.mIdentity.mUid,
record.mReceiver.mIdentity.mPackageName,
record.mReceiver.mAllowedResolutionLevel)) {
LocationRequest locationRequest = record.mRealRequest;
long interval = locationRequest.getInterval();
if (!isThrottlingExemptLocked(record.mReceiver.mIdentity)) {
if (!record.mIsForegroundUid) {
interval = Math.max(interval, backgroundThrottleInterval);
}
if (interval != locationRequest.getInterval()) {
locationRequest = new LocationRequest(locationRequest);
locationRequest.setInterval(interval);
}
}
record.mRequest = locationRequest;
providerRequest.locationRequests.add(locationRequest);
if (!locationRequest.isLowPowerMode()) {
providerRequest.lowPowerMode = false;
}
if (interval < providerRequest.interval) {
providerRequest.reportLocation = true;
providerRequest.interval = interval;
}
}
}
}
if (providerRequest.reportLocation) {
// calculate who to blame for power
// This is somewhat arbitrary. We pick a threshold interval
// that is slightly higher that the minimum interval, and
// spread the blame across all applications with a request
// under that threshold.
long thresholdInterval = (providerRequest.interval + 1000) * 3 / 2;
for (UpdateRecord record : records) {
if (isCurrentProfile(UserHandle.getUserId(record.mReceiver.mIdentity.mUid))) {
LocationRequest locationRequest = record.mRequest;
// Don't assign battery blame for update records whose
// client has no permission to receive location data.
if (!providerRequest.locationRequests.contains(locationRequest)) {
continue;
}
if (locationRequest.getInterval() <= thresholdInterval) {
if (record.mReceiver.mWorkSource != null
&& isValidWorkSource(record.mReceiver.mWorkSource)) {
worksource.add(record.mReceiver.mWorkSource);
} else {
// Assign blame to caller if there's no WorkSource associated with
// the request or if it's invalid.
worksource.add(
record.mReceiver.mIdentity.mUid,
record.mReceiver.mIdentity.mPackageName);
}
}
}
}
}
}
if (D) Log.d(TAG, "provider request: " + provider + " " + providerRequest);
// 最终调用Provider的setRequest方法,进入到Provider的内部流程
p.setRequest(providerRequest, worksource);
}

总结

总结下LocationManagerService里面初始化的流程

graph TD
systemRunning --> loadProvidersLocked

subgraph loadProvidersLockedProcess
loadProvidersLocked --> loadPassiveProvider
loadPassiveProvider --> loadGnssProvider
loadGnssProvider --> checkFusedProvider
checkFusedProvider --> bindNetworkProvider
bindNetworkProvider --> bindGeocoderProvider
bindGeocoderProvider --> bindGeofenceProvider
bindGeofenceProvider --> bindHardwareActivityRecogneition
bindHardwareActivityRecogneition --> updateProvidersLocked
end

subgraph updateProvidersLockedProcess
updateProvidersLocked --> updateProviderListenersLocked
updateProviderListenersLocked --> provider.enable
provider.enable --> applyRequirementsLocked
applyRequirementsLocked --> provider.setRequest
end

备注:

上面代码基于Android9 AOSP源码

Android LocationManagerService启动(一)的更多相关文章

  1. 设置Android Studio启动时可选最近打开过的工程

    Android Studio启动时,默认会打开最近关闭的工程. 如果想Android Studio在启动时,打开欢迎界面(Welcome to Android Studio界面),则可以通过设置Set ...

  2. Android开机启动Activity或者Service方法

    本文出自 “Bill_Hoo专栏” 博客,请务必保留此出处http://billhoo.blog.51cto.com/2337751/761230 这段时间在做Android的基础开发,现在有一需求是 ...

  3. [FMX] Android APP 启动黑屏优化补丁

    使用说明 *************************************************** Android APP 启动黑屏优化补丁 作者: Swish, YangYxd 201 ...

  4. Windows自带Android模拟器启动失败

    Windows自带Android模拟器启动失败 错误信息:[Critical] XDE Exit Code: InvalidArguments (3)XDE执行的第三个参数为设置内存值,形式为/mem ...

  5. Android的启动模式(上)

    1. 基本介绍 大家平时只要懂一点Android知识的话,都一定会知道,一个应用的组成,往往包含了许多的activity组件,每个activity都应该围绕用户的特定动作进行跳转设计.比如说,一个电话 ...

  6. Android学习笔记1 android adb启动失败问题 adb server is out of date. killing...

    下面是Android的学习笔记,原文地址. 我是使用adb devices出现如下红字错误, 使用第一种方法方法,结果关掉豌豆荚就可以了. android adb启动失败问题 adb server i ...

  7. studio_ 优化Android Studio 启动、编译和运行速度?

    http://www.admin10000.com/document/6842.html: 作为一名 Android 程序员,选择一个好的 IDE 工具可以使开发变得非常高效,很多程序员喜欢使用 Go ...

  8. Android WIFI 启动流程(TIP^^)

    前几天因为解决一堆Bug,没时间写.我不会每天都写,就是为了存档一些资料. 内容来源:工作中接触到的+高手博客+文档(Books)=自己理解 仅限参考^^ 此博客是上一个<<Android ...

  9. android sdk启动报错error: could not install *smartsocket* listener: cannot bind to 127.0.0.1:5037:

    android sdk启动报错error: could not install *smartsocket* listener: cannot bind to 127.0.0.1:5037: 问题原因: ...

随机推荐

  1. OAuth2.0的四种授权模式(转)

    1. OAuth2简易实战(一)-四种模式 1.1. 隐式授权模式(Implicit Grant) 第一步:用户访问页面时,重定向到认证服务器. 第二步:认证服务器给用户一个认证页面,等待用户授权. ...

  2. java零基础之--【Lombok】简化类设计神器

    I1. 在类设计中我们必不可少的要进行属性定义,构造方法,setter/getter方法,toString方法定义,如果在设计项目中属性过多则会影响类的阅读性. Lombok作为第三方插件,很好的解决 ...

  3. vue中Echarts的使用-自选效果

    由于项目要求使用数据图,于是我选择了我们的Echarts用来实现效果 一:全局安装Echarts npm install echarts --save(这个安装的是最新的版本有时候回报init未定义) ...

  4. Java内存模型精讲

    1.JAVA 的并发模型 共享内存模型 在共享内存的并发模型里面,线程之间共享程序的公共状态,线程之间通过读写内存中公共状态来进行隐式通信 该内存指的是主内存,实际上是物理内存的一小部分 2.JAVA ...

  5. C++把数字排序

    C++把数字排序 描述 思路 代码 描述 如题,详细如下: 输入不超过1024个数字,以特殊数字结尾,如(-999),把数字从小到大排序. 思路 目前,我们有两种思路可以写: 1是 在输入的时候,排序 ...

  6. ORA-28001: the password has expired解决方法

    Oracle提示错误消息ORA-28001: the password has expired,是由于Oracle11G的新特性所致, Oracle11G创建用户时缺省密码过期限制是180天(即6个月 ...

  7. 【详细】Python基础(一)

    @ 目录 前言 1. Python环境的搭建 1.1 python解释器的安装 1.2 pycharm的安装 2. Python基础语法 2.1 基本语法 2.2 数据类型 2.3 标识符与关键字 2 ...

  8. 容器编排系统K8s之crd资源

    前文我们了解了k8s节点污点和pod的对节点污点容忍度相关话题,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14255486.html:今天我们来聊一下扩展 ...

  9. SpringBoot 集成Shiro之使用Redis缓存授权认证信息

    因为用户认证与授权需要从数据库中查询并验证信息,但是对于权限很少改变的情况,这样不断从数据库中查询角色验证权限,对整个系统的开销很大,对数据库压力也随之增大.因此可以将用户认证和授权信息都缓存起来,第 ...

  10. 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs

    前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...