浏览器版本低!无法浏览完整内容,建议升级或更换浏览器。
Android 8.0 开发须知
下载开发文档

Android 8.0系统增加了很多新特性,对位置影响较大的特性就是增强了对后台定位的限制。
Android 8.0系统为实现降低功耗,无论应用的目标SDK版本为多少,Android 8.0系统都会对后台应用获取用户当前位置的频率进行限制,只允许后台应用每小时接收几次位置更新。
根据Android 8.0的开发指引,为了适配这一系统特性,百度地图定位SDK新增加接口。它的实现原理就是开发者通过新接口生成一个前台服务通知,使得开发者自己的应用退到后台的时候,仍有前台通知在,规避了Android 8.0系统对后台定位的限制。
注意:如果您的App在退到后台时本身就有前台服务通知,则无需按照本章节的介绍做适配注。

核心实现代码如下,详细代码请参考Demo。

//开启前台定位服务:
Notification.Builder builder = new Notification.Builder (MainActivity.this.getApplicationContext());
//获取一个Notification构造器
Intent nfIntent = new Intent(MainActivity.this.getApplicationContext(), MainActivity.class);
builder.setContentIntent(PendingIntent.getActivity(MainActivity.this, 0, nfIntent, 0)) // 设置PendingIntent
.setContentTitle("正在进行后台定位") // 设置下拉列表里的标题
.setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标
.setContentText("后台定位通知") // 设置上下文内容
.setAutoCancel(true)
.setWhen(System.currentTimeMillis()); // 设置该通知发生的时间
Notification notification = null;
notification = builder.build();
notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
mLocClient.enableLocInForeground(1001, notification);// 调起前台定位
//停止前台定位服务:
mLocClient.disableLocInForeground(true);// 关闭前台定位,同时移除通知栏

在Android 8.0系统中,Notification增加了新的接口,建议开发者针对Android 8.0做特别适配,详细代码可参考Demo。

注意:本接口不一定只在Android 8.0系统上使用,在其他版本Android系统中使用也可最大程度的增加定位进程的存活率,提升后台定位效果。

上一篇

Android 9.0 开发须知

下一篇

Android 7.0 开发须知

本篇文章对您是否有帮助?