实现高德地图后台持续定位

 高德地图实现在后台持续定位,当然需要服务,但是现在已经无法在杀死APP的时候仍然持续获取了,我这种形式只能在APP没被杀死的时候持续获取,获取到定位信息后你可以回调出去,下面就是我这个服务

public class LocationService extends Service {

    private static final String TAG = "LocationService";
    //声明AMapLocationClient类对象
    AMapLocationClient mLocationClient = null;
    // 声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;

    public LocationService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        getPosition();
    }

    public void getPosition() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        // 设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        // 初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        // 设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(60000);
        // 获取一次定位结果: //该方法默认为false。
        mLocationOption.setOnceLocation(false);
        mLocationOption.setOnceLocationLatest(false);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        // 启动定位
        mLocationClient.startLocation();
    }

    // 声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation == null) {
                Log.i(TAG, "amapLocation is null!");
                return;
            }
            if (amapLocation.getErrorCode() != 0) {
                Log.i(TAG, "amapLocation has exception errorCode:" + amapLocation.getErrorCode());
                return;
            }
            Double longitude = amapLocation.getLongitude();//获取经度
            Double latitude = amapLocation.getLatitude();//获取纬度
            String longitudestr = String.valueOf(longitude);
            String latitudestr = String.valueOf(latitude);
            Log.i(TAG, "longitude:" + longitude + ",latitude:" + latitude);
        }
    };
}

 添加这个服务需要在清单文件注册

<service
            android:name=".service.LocationService"
            android:enabled="true"
            android:exported="true" />

        <!--处理bug:amapLocation has exception errorCode:10-->
        <service android:name="com.amap.api.location.APSService" />
    </application>

如果不加 APSService这个会报错,加上就可以了

然后就是去启动服务了

 public void startAlarm() {
        //首先获得系统服务
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        //设置闹钟的意图,我这里是去调用一个服务,该服务功能就是获取位置并且上传
        Intent intent = new Intent(this, LocationService.class);
        PendingIntent pendSender = PendingIntent.getService(this, 0, intent, 0);
        am.cancel(pendSender);
        //AlarmManager.RTC_WAKEUP ;这个参数表示系统会唤醒进程;设置的间隔时间是1分钟
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60 * 1000, pendSender);
    }

猜你喜欢

转载自blog.csdn.net/lovelixue/article/details/85371817