核心类/接口
类 | 接口 | 说明 | 版本 |
---|
AMapLocationClient | startLocation(); | 启动定位 | V2.0.0版本起 | | setLocationOption(mLocationOption); | 给定位客户端设置参数 | V2.0.0版本起 | AMapLocationClientOption | setInterval(long time); | 设置连续定位时间间隔 | V2.0.0版本起 | AMapLocationListener | onLocationChanged(AMapLocation amapLocation) ; | 监听器回调方法 | V2.0.0版本起 |
核心难点
首先在本地服务中启动连续定位功能,通过设置一个Alarm定期对本地服务进行周期唤起,从而达到后台持续定位的效果。
1、在本地服务里启动连续定位:
//在activity中启动自定义本地服务LocationService
getApplicationContext().startService(new Intent(this, LocationService.class));
//在LocationService中启动定位
mLocationClient = new AMapLocationClient(this.getApplicationContext());
mLocationOption = new AMapLocationClientOption();
// 使用连续定位
mLocationOption.setOnceLocation(false);
// 每10秒定位一次
mLocationOption.setInterval(10 * 1000);
mLocationClient.setLocationOption(mLocationOption);
mLocationClient.setLocationListener(locationListener);
mLocationClient.startLocation();
2、创建并启动Alarm。
//创建Alarm并启动
Intent intent = new Intent("LOCATION_CLOCK");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
// 每五秒唤醒一次
long second = 15 * 1000;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), second, pendingIntent);
3、通过Alarm启动本地服务,如果定位停止,在服务中再次启动。
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("LOCATION_CLOCK")) {
Log.e("ggb", "--->>> onReceive LOCATION_CLOCK");
Intent locationIntent = new Intent(context, LocationService.class);
context.startService(locationIntent);
}
}
}
然后,定期在CPU休眠之前进行屏幕点亮操作。这个操作会导致cpu无法休眠耗电量增加,请谨慎使用。
if (powerManager == null) {
//针对熄屏后cpu休眠导致的无法联网、定位失败问题,通过定期点亮屏幕实现联网,本操作会导致cpu无法休眠耗电量增加,谨慎使用
powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl=powerManager
.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
wl.acquire();
//点亮屏幕
wl.release();
//释放
}
DEMO
1、首先在自定义的MyApplication里面去开始AlarmManager定时闹钟服务
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
startAlarm();
}
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 这个参数表示系统会唤醒进程;我设置的间隔时间是10分钟
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10*60*1000, pendSender);
}
}
2、处理定位的服务:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.model.LatLng;
import com.orhanobut.logger.Logger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by Administrator on 2019/3/11
* <p>
* desc:
*/
public class LocationService extends Service {
private static final String TAG = "LocationService";
//声明AMapLocationClient类对象
AMapLocationClient mLocationClient;
// 声明AMapLocationClientOption对象
publib33Cb3C36FWD6G6.{zr撅Dr33CYX{z33Cb3C36FWDD6X{zr撅Dr33C[Xx+yNK33Cb3C36FWD~XXx+yDhVRIh6fc3W'$ifc3I8 rb33CW'&3Cb33C6F'&W'$6b33Cb3C36FWDW'&FRb3C3b33CW'$b33Cb3C36FWDW'&fТТТ6&S.i^Ib7fc6F'&W'$6rW'$U>Xk>ikk9Rb7fc3KyJKK>zyE4X[Y{K4XXfcXiyG6Y(z[{yK^IyG6Kj~ZyK>WK;Jb7fc8#&R673&66V&27FF27G&46B6BG'6vTb3c6BE6vTW"E6vT6BE6vTR6vTW"EU$U2'FU6W'Bb3cvW&W5FT'&76vTFvW7Bb3c76vTFvW7BDb33C4b33C'FUV&4Wb3cvW7B6W'B7G&VffW"W7G&b3c7G&VffW"fb3cfV&4Wb3C3b3C37G&VG&b3cvW"W7G&dbfV&4WWW$66R6bVG&b3cb3cW7G&Vb33Cb33CW7G&VVG&W7G&Vb33Cb33CТ7G&&W7Vb3cW7G&F7VXK4XZh.iY.iXi.XX萢r撅DrW7V7G&&WGW&W7V'7G&&W7V6F66vTW"fW6WFRF6G&6R6F6V66WFRF6G&6RТ&WGW&VS&S
|