Android源码分析(十五)----GPS冷启动实现原理分析
2024-08-24 20:40:19
一:原理分析
- 主要sendExtraCommand方法中传递两个参数, 根据如下源码可以知道第一个参数传递
delete_aiding_data
,第二个参数传递null
即可。
@Override
public boolean sendExtraCommand(String command, Bundle extras) {
long identity = Binder.clearCallingIdentity();
boolean result = false;
if ("delete_aiding_data".equals(command)) {
result = deleteAidingData(extras);
} else if ("force_time_injection".equals(command)) {
sendMessage(INJECT_NTP_TIME, 0, null);
result = true;
} else if ("force_xtra_injection".equals(command)) {
if (mSupportsXtra) {
xtraDownloadRequest();
result = true;
}
} else {
Log.w(TAG, "sendExtraCommand: unknown command " + command);
}
Binder.restoreCallingIdentity(identity);
return result;
}
private boolean deleteAidingData(Bundle extras) {
int flags;
if (extras == null) {
flags = GPS_DELETE_ALL;
} else {
flags = 0;
if (extras.getBoolean("ephemeris")) flags |= GPS_DELETE_EPHEMERIS;
if (extras.getBoolean("almanac")) flags |= GPS_DELETE_ALMANAC;
if (extras.getBoolean("position")) flags |= GPS_DELETE_POSITION;
if (extras.getBoolean("time")) flags |= GPS_DELETE_TIME;
if (extras.getBoolean("iono")) flags |= GPS_DELETE_IONO;
if (extras.getBoolean("utc")) flags |= GPS_DELETE_UTC;
if (extras.getBoolean("health")) flags |= GPS_DELETE_HEALTH;
if (extras.getBoolean("svdir")) flags |= GPS_DELETE_SVDIR;
if (extras.getBoolean("svsteer")) flags |= GPS_DELETE_SVSTEER;
if (extras.getBoolean("sadata")) flags |= GPS_DELETE_SADATA;
if (extras.getBoolean("rti")) flags |= GPS_DELETE_RTI;
if (extras.getBoolean("celldb-info")) flags |= GPS_DELETE_CELLDB_INFO;
if (extras.getBoolean("all")) flags |= GPS_DELETE_ALL;
}
if (flags != 0) {
native_delete_aiding_data(flags);
return true;
}
return false;
}
根据flags值GPS_DELETE_ALL
清除数据
static void android_location_GpsLocationProvider_delete_aiding_data(JNIEnv* /* env */,
jobject /* obj */,
jint flags)
{
if (sGpsInterface)
sGpsInterface->delete_aiding_data(flags);
}
/**
* Specifies that the next call to start will not use the
* information defined in the flags. GPS_DELETE_ALL is passed for
* a cold start.
*/
void (*delete_aiding_data)(GpsAidingData flags);
二:实现方式
java层主要代码如下:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Bundle bundle = null;
locationManager.sendExtraCommand(LocationManager.GPS_PROVIDER, "force_xtra_injection", bundle);
locationManager.sendExtraCommand(LocationManager.GPS_PROVIDER, "force_time_injection", bundle);
//GPS cold start
locationManager.sendExtraCommand(LocationManager.GPS_PROVIDER, "delete_aiding_data", bundle);
所需权限配置如下, 6.0版本可能需要动态申请权限。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
喜欢源码分析系列可参考其他文章:
Android源码分析(一)-----如何快速掌握Android编译文件
Android源码分析(二)-----如何编译修改后的framework资源文件
Android源码分析(三)-----系统框架设计思想
Android源码分析(四)-----Android源码编译及刷机步骤
Android源码分析(五)-----如何从架构师的角度去设计Framework框架
最新文章
- 安装Ubuntu的那些事儿(续)
- day5 -指针
- Spring3中的mvc:interceptors标签配置拦截器
- Linux: xclip,pbcopy,xsel用法 terminal 复制粘帖 (mac , ubuntu)
- 3.HelloWorld
- sql 清空所有数据
- linux网络相关
- 【转】SqlLite .Net 4.0 System.IO.FileLoadException”类型的未经处理的异常出现在XXX
- Top 10 Books For Advanced Level Java Developers
- linux中一些常用的目录简要说明
- app锁定屏幕方向,某一个界面支持屏幕旋转~
- kafka7 探索生产者同步or异步发送消息
- Struts2 使用Jquery+ajax 文件上传
- css 如何实现图片等比例缩放
- ubuntu下配置华为交换机s2016
- 谷歌浏览器network请求时间(stalled,DNS Lookup,Waiting)分析以及解决方案
- Android开发笔记——ListView模块、缓存及性能
- https通讯原理
- ES6/ES2015核心内容(上)
- HTML— 弹出遮盖层