http://lbsyun.baidu.com/index.php?title=%E5%90%8E%E5%8F%B0%E6%8C%81%E7%BB%AD%E5%AE%9A%E4%BD%8D
1.创建前台通知服务
修改:版本判断移至 NotificationUtils
Notification mNotification;
void initNotification() {
//设置后台定位
//Android 8.0 及以上使用 NotificationUtils
String title = "轨迹地图";
String text = "正在定位";
Notification.Builder builder;
NotificationUtils notificationUtils = new NotificationUtils(this);
builder = notificationUtils.getAndroidChannelNotification(title, text);
mNotification = builder.build();
mNotification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
}
2.将sdk服务设置为前台通知服务
mClient.enableLocInForeground(1, mNotification);
3.创建后台定位监听
class MyLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
// 此处获取后台定位数据
}
}
4.配置后台连续定位参数并启动定位
// 创建定位客户端
mClient = new LocationClient(this);
myLocationListener = new MyLocationListener();
// 注册定位监听
mClient.registerLocationListener(myLocationListener);
LocationClientOption mOption = new LocationClientOption();
// 可选,默认0,即仅定位一次,设置发起连续定位请求的间隔需要大于等于1000ms才是有效的
mOption.setScanSpan(2000);
// 可选,默认gcj02,设置返回的定位结果坐标系,如果配合百度地图使用,建议设置为bd09ll;
mOption.setCoorType("bd09ll");
// 可选,默认false,设置是否开启Gps定位
mOption.setOpenGps(true);
// 设置定位参数
mClient.setLocOption(mOption);
// 启动定位
mClient.start();
5.NotificationUtils.java 内容文档没有,在Demo里面
修改后支持多个Activity跳转
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
public class NotificationUtils extends ContextWrapper {
private NotificationManager mManager;
public static final String ANDROID_CHANNEL_ID = "com.hty.locusmap";
public static final String ANDROID_CHANNEL_NAME = "ANDROID CHANNEL";
Context context;
public NotificationUtils(Context context) {
super(context);
this.context = context;
if (Build.VERSION.SDK_INT >= 26) {
createChannels();
}
}
public void createChannels() {
NotificationChannel notificationChannel = new NotificationChannel(ANDROID_CHANNEL_ID, ANDROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setLightColor(Color.GREEN);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(notificationChannel);
}
private NotificationManager getManager() {
if (mManager == null) {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
public Notification.Builder getAndroidChannelNotification(String title, String text) {
Intent intent = new Intent(context, context.getClass());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification.Builder builder;
if (Build.VERSION.SDK_INT >= 26) {
builder = new Notification.Builder(getApplicationContext(), ANDROID_CHANNEL_ID);
} else {
builder = new Notification.Builder(context);
}
builder.setContentTitle(title)
.setContentText(text)
.setSmallIcon(android.R.drawable.stat_notify_more)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
return builder;
}
}
6.应用省电策略设置:无限制