Android P 代码:http://androidxref.com/9.0.0_r3/
上一篇幅我们梳理了位置开关打开的一个流程,这里我们梳理下请求Gps定位的一个流程
这里我们假设一个app发起Gps定位,首先调用 LocationManager 接口,通过AIDL最终调用到 LocationManagerService 里面的 requestLocationUpdates 方法:
553 @RequiresPermission(anyOf = { ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) 554 public void requestLocationUpdates(String provider, long minTime, float minDistance, 555 LocationListener listener) { 556 checkProvider(provider); 557 checkListener(listener); 558 559 LocationRequest request = LocationRequest.createFromDeprecatedProvider( ---> 封装成LocationRequest,作为参数传下去 560 provider, minTime, minDistance, false); 561 requestLocationUpdates(request, listener, null, null); 562 }
2061 public void requestLocationUpdates(LocationRequest request, ILocationListener listener, 2062 PendingIntent intent, String packageName) { 2063 if (request == null) request = DEFAULT_LOCATION_REQUEST; 2064 checkPackageName(packageName); 2065 int allowedResolutionLevel = getCallerAllowedResolutionLevel(); ---> 这里指的就是 fine、coarse、no location data 三种 2066 checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel, 2067 request.getProvider()); 2068 WorkSource workSource = request.getWorkSource(); 2069 if (workSource != null && !workSource.isEmpty()) { 2070 checkDeviceStatsAllowed(); 2071 } 2072 boolean hideFromAppOps = request.getHideFromAppOps(); 2073 if (hideFromAppOps) { 2074 checkUpdateAppOpsAllowed(); 2075 } 2076 boolean callerHasLocationHardwarePermission = 2077 mContext.checkCallingPermission(android.Manifest.permission.LOCATION_HARDWARE) 2078 == PERMISSION_GRANTED; 2079 LocationRequest sanitizedRequest = createSanitizedRequest(request, allowedResolutionLevel, 2080 callerHasLocationHardwarePermission); 2081 2082 final int pid = Binder.getCallingPid(); 2083 final int uid = Binder.getCallingUid(); 2084 // providers may use public location API's, need to clear identity 2085 long identity = Binder.clearCallingIdentity(); 2086 try { 2087 // We don't check for MODE_IGNORED here; we will do that when we go to deliver 2088 // a location. 2089 checkLocationAccess(pid, uid, packageName, allowedResolutionLevel); 2090 2091 synchronized (mLock) { 2092 Receiver recevier = checkListenerOrIntentLocked(listener, intent, pid, uid, 2093 packageName, workSource, hideFromAppOps); 2094 requestLocationUpdatesLocked(sanitizedRequest, recevier, pid, uid, packageName); ---> 继续看下这个 2095 } 2096 } finally { 2097 Binder.restoreCallingIdentity(identity); 2098 } 2099 }
2101 private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver, 2102 int pid, int uid, String packageName) { 2103 // Figure out the provider. Either its explicitly request (legacy use cases), or 2104 // use the fused provider 2105 if (request == null) request = DEFAULT_LOCATION_REQUEST; 2106 String name = request.getProvider(); ---> 获取此时的provide,下面就是根据这个provide决定走GPS还是走network,所以不能为空 2107 if (name == null) { 2108 throw new IllegalArgumentException("provider name must not be null"); 2109 }

最低0.47元/天 解锁文章
1895

被折叠的 条评论
为什么被折叠?



