没有要求用第三方的定位,我就用Gps了,反正只是上传经纬度到服务器,根据项目我是在service里写的
创建一个service,manifest里记得配置下
public class GpsService extends Service {
private LocationManager mLoactionManager;
protected CompositeDisposable mDisposable = new CompositeDisposable();
protected MaYiApiService apiService = RetrofitHelper.getApiService();
public static final String TAG = "GpsService";
private Location location;
String mToken;
@Override
public void onCreate() {
Log.d(TAG, "onCreate() executed");
super.onCreate();
SharedPreferences sp = getSharedPreferences("userInfo", MODE_PRIVATE);
mToken = sp.getString("Token", "");
mLoactionManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//判断GPS是否正常启动
if (!mLoactionManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
CommonUtils.showToast(getApplicationContext(), "请开启GPS导航...");
//返回开启GPS导航设置界面
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
return;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand() executed");
//为获取地理位置信息时设置查询条件
String bestProvider = mLoactionManager.getBestProvider(getCriteria(), true);
//获取位置信息
//如果不设置查询要求,getLastKnownLocation方法传人的参数为LocationManager.GPS_PROVIDER
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
CommonUtils.showToast(getApplicationContext(), "请添加gps权限");
return flags;
}
Location location = mLoactionManager.getLastKnownLocation(bestProvider);
CommonUtils.showToast(getApplicationContext(),"hah"+location);
showLOcation(location);
mLoactionManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,600000,1, locationListener);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy() executed");
super.onDestroy();
mLoactionManager.removeUpdates(locationListener);
}
//位置监听
private LocationListener locationListener = new LocationListener() {
/**
* 位置信息变化时触发
*/
public void onLocationChanged(Location location) {
showLOcation(location);
Log.i(TAG, "时间:" + location.getTime());
Log.i(TAG, "经度:" + location.getLongitude());
Log.i(TAG, "纬度:" + location.getLatitude());
Log.i(TAG, "海拔:" + location.getAltitude());
}
/**
* GPS状态变化时触发
*/
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
//GPS状态为可见时
case LocationProvider.AVAILABLE:
Log.i(TAG, "当前GPS状态为可见状态");
break;
//GPS状态为服务区外时
case LocationProvider.OUT_OF_SERVICE:
Log.i(TAG, "当前GPS状态为服务区外状态");
break;
//GPS状态为暂停服务时
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.i(TAG, "当前GPS状态为暂停服务状态");
break;
}
}
/**
* GPS开启时触发
*/
public void onProviderEnabled(String provider) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
CommonUtils.showToast(getApplicationContext(), "请添加gps权限");
return;
}
Location location = mLoactionManager.getLastKnownLocation(provider);
showLOcation(location);
}
/**
* GPS禁用时触发
*/
public void onProviderDisabled(String provider) {
showLOcation(null);
}
};
private void showLOcation(Location location){
if (location!=null){
PostInfoBean.PostInfoGps postInfoGps = new PostInfoBean.PostInfoGps();
postInfoGps.setLat((float) location.getLatitude());
postInfoGps.setLon((float)location.getLongitude());
String postInfoStr = GsonSingle.getInstance().toJson(postInfoGps);
RequestBody body = RequestBody.create(MediaType.parse("application/json"), postInfoStr);
//联网上传经纬度
mDisposable.add(apiService.getGps(mToken,body).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Messagebean>() {
@Override
public void accept(@NonNull Messagebean s) throws Exception {
}
}, new Consumer<Throwable>() {
@Override
public void accept(@NonNull Throwable throwable) throws Exception {
}
}));
}else{
CommonUtils.showToast(getApplicationContext(),"没有获取到经纬度");
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
/**
* 返回查询条件
* @return
*/
private Criteria getCriteria(){
Criteria criteria=new Criteria();
//设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//设置是否要求速度
criteria.setSpeedRequired(false);
// 设置是否允许运营商收费
criteria.setCostAllowed(false);
//设置是否需要方位信息
criteria.setBearingRequired(false);
//设置是否需要海拔信息
criteria.setAltitudeRequired(false);
// 设置对电源的需求
criteria.setPowerRequirement(Criteria.POWER_LOW);
return criteria;
}
}
在需要启动的时候调用
Intent startIntent = new Intent(context, GpsService.class);
startService(startIntent);
不用的就停止
Intent startIntent = new Intent(context, GpsService.class);
stopService(startIntent);