安卓百度地图API后台定位

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.应用省电策略设置:无限制

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值