200 lines
11 KiB
Java
200 lines
11 KiB
Java
package com.fuying.sn.manager;
|
||
|
||
import android.annotation.SuppressLint;
|
||
import android.content.Context;
|
||
import android.provider.Settings;
|
||
import android.util.Log;
|
||
|
||
import com.baidu.location.BDAbstractLocationListener;
|
||
import com.baidu.location.BDLocation;
|
||
import com.baidu.location.LocationClient;
|
||
import com.baidu.location.LocationClientOption;
|
||
import com.fuying.sn.utils.SPUtils;
|
||
|
||
public class AmapManager {
|
||
private static final String TAG = AmapManager.class.getSimpleName();
|
||
@SuppressLint("StaticFieldLeak")
|
||
private static AmapManager sInstance;
|
||
private Context mContext;
|
||
@SuppressLint("StaticFieldLeak")
|
||
private LocationClient mLocationClient = null;
|
||
private LocationClientOption mOption;
|
||
|
||
private AmapManager(Context context) {
|
||
this.mContext = context;
|
||
// Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "+gps");
|
||
Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "+network");
|
||
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.ASSISTED_GPS_ENABLED, 1);
|
||
initAmap();
|
||
}
|
||
|
||
public static void init(Context context) {
|
||
if (context == null) {
|
||
throw new RuntimeException("Context is NULL");
|
||
}
|
||
if (sInstance == null) {
|
||
sInstance = new AmapManager(context);
|
||
}
|
||
}
|
||
|
||
public static AmapManager getInstance() {
|
||
if (sInstance == null) {
|
||
throw new IllegalStateException("You must be init AmapManager first");
|
||
}
|
||
return sInstance;
|
||
}
|
||
|
||
public void initAmap() {
|
||
if (mLocationClient == null) {
|
||
mLocationClient = new LocationClient(mContext);
|
||
}
|
||
// Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "+gps");
|
||
Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "+network");
|
||
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.ASSISTED_GPS_ENABLED, 1);
|
||
mLocationClient.setLocOption(getDefaultLocationClientOption());
|
||
mLocationClient.registerLocationListener(mListener);
|
||
mLocationClient.stop();
|
||
mLocationClient.start();
|
||
}
|
||
|
||
public LocationClient getLocationClient() {
|
||
if (mLocationClient == null) {
|
||
initAmap();
|
||
}
|
||
return mLocationClient;
|
||
}
|
||
|
||
/***
|
||
*
|
||
* @return DefaultLocationClientOption 默认O设置
|
||
*/
|
||
public LocationClientOption getDefaultLocationClientOption() {
|
||
if (mOption == null) {
|
||
mOption = new LocationClientOption();
|
||
mOption.setCoorType("bd09ll"); // 可选,默认gcj02,设置返回的定位结果坐标系,如果配合百度地图使用,建议设置为bd09ll;
|
||
mOption.setScanSpan(0); // 可选,默认0,即仅定位一次,设置发起连续定位请求的间隔需要大于等于1000ms才是有效的
|
||
mOption.setIsNeedAddress(true); // 可选,设置是否需要地址信息,默认不需要
|
||
mOption.setIsNeedLocationDescribe(true); // 可选,设置是否需要地址描述
|
||
mOption.setNeedDeviceDirect(false); // 可选,设置是否需要设备方向结果
|
||
mOption.setLocationNotify(false); // 可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
|
||
mOption.setIgnoreKillProcess(true); // 可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop
|
||
mOption.setIsNeedLocationDescribe(true); // 可选,默认false,设置是否需要位置语义化结果,可以在BDLocation
|
||
mOption.setIsNeedLocationPoiList(true); // 可选,默认false,设置是否需要POI结果,可以在BDLocation
|
||
mOption.SetIgnoreCacheException(false); // 可选,默认false,设置是否收集CRASH信息,默认收集
|
||
mOption.setLocationMode(LocationClientOption.LocationMode.Battery_Saving); // 可选,默认高精度,设置定位模式,高精度,低功耗,仅设备,模糊
|
||
mOption.setIsNeedAltitude(false); // 可选,默认false,设置定位时是否需要海拔信息,默认不需要,除基础定位版本都可用
|
||
// 可选,设置首次定位时选择定位速度优先还是定位准确性优先,默认为速度优先
|
||
// mOption.setFirstLocType(LocationClientOption.FirstLocType.SPEED_IN_FIRST_LOC);
|
||
}
|
||
return mOption;
|
||
}
|
||
|
||
|
||
/*****
|
||
*
|
||
* 定位结果回调,重写onReceiveLocation方法,可以直接拷贝如下代码到自己工程中修改
|
||
*
|
||
*/
|
||
private BDAbstractLocationListener mListener = new BDAbstractLocationListener() {
|
||
|
||
/**
|
||
* 定位请求回调函数
|
||
* @param location 定位结果
|
||
*/
|
||
@Override
|
||
public void onReceiveLocation(BDLocation location) {
|
||
if (null != location) {
|
||
switch (location.getLocType()) {
|
||
case BDLocation.TypeGpsLocation:// GPS定位结果
|
||
case BDLocation.TypeNetWorkLocation:// 网络定位结果
|
||
case BDLocation.TypeOffLineLocation:// 离线定位结果
|
||
Log.e(TAG, "onLocationChanged: " + "定位成功");
|
||
Log.e(TAG, "onLocationChanged: " + location.getAddrStr() + location.getLocationDescribe());
|
||
SPUtils.put(mContext, "AmapAddress", location.getAddrStr() + location.getLocationDescribe());
|
||
SPUtils.put(mContext, "longitude", location.getLongitude());
|
||
SPUtils.put(mContext, "latitude", location.getLatitude());
|
||
SPUtils.put(mContext, "AmapError", "-");
|
||
break;
|
||
case BDLocation.TypeServerError:
|
||
Log.e(TAG, "onReceiveLocation: " + "服务端网络定位失败");
|
||
SPUtils.put(mContext, "AmapError", "服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
|
||
break;
|
||
case BDLocation.TypeNetWorkException:
|
||
Log.e(TAG, "onReceiveLocation: " + "网络不同导致定位失败,请检查网络是否通畅");
|
||
SPUtils.put(mContext, "AmapError", "网络不同导致定位失败,请检查网络是否通畅");
|
||
break;
|
||
case BDLocation.TypeCriteriaException:
|
||
Log.e(TAG, "onReceiveLocation: " + "无法获取有效定位依据导致定位失败");
|
||
SPUtils.put(mContext, "AmapError", "无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
|
||
break;
|
||
default:
|
||
}
|
||
}
|
||
Log.e(TAG, "AmapAddress: " + (String) SPUtils.get(mContext, "AmapAddress", "-"));
|
||
Log.e(TAG, "AmapError: " + (String) SPUtils.get(mContext, "AmapError", "-"));
|
||
Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "-gps");
|
||
Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "-network");
|
||
Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "");
|
||
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.ASSISTED_GPS_ENABLED, 0);
|
||
}
|
||
|
||
@Override
|
||
public void onConnectHotSpotMessage(String s, int i) {
|
||
super.onConnectHotSpotMessage(s, i);
|
||
}
|
||
|
||
/**
|
||
* 回调定位诊断信息,开发者可以根据相关信息解决定位遇到的一些问题
|
||
* @param locType 当前定位类型
|
||
* @param diagnosticType 诊断类型(1~9)
|
||
* @param diagnosticMessage 具体的诊断信息释义
|
||
*/
|
||
@Override
|
||
public void onLocDiagnosticMessage(int locType, int diagnosticType, String diagnosticMessage) {
|
||
super.onLocDiagnosticMessage(locType, diagnosticType, diagnosticMessage);
|
||
// int tag = 2;
|
||
StringBuffer sb = new StringBuffer(256);
|
||
sb.append("诊断结果: ");
|
||
if (locType == BDLocation.TypeNetWorkLocation) {
|
||
if (diagnosticType == 1) {
|
||
sb.append("网络定位成功,没有开启GPS,建议打开GPS会更好");
|
||
sb.append("\n" + diagnosticMessage);
|
||
} else if (diagnosticType == 2) {
|
||
sb.append("网络定位成功,没有开启Wi-Fi,建议打开Wi-Fi会更好");
|
||
sb.append("\n" + diagnosticMessage);
|
||
}
|
||
} else if (locType == BDLocation.TypeOffLineLocationFail) {
|
||
if (diagnosticType == 3) {
|
||
sb.append("定位失败,请您检查您的网络状态");
|
||
sb.append("\n" + diagnosticMessage);
|
||
}
|
||
} else if (locType == BDLocation.TypeCriteriaException) {
|
||
if (diagnosticType == 4) {
|
||
sb.append("定位失败,无法获取任何有效定位依据");
|
||
sb.append("\n" + diagnosticMessage);
|
||
} else if (diagnosticType == 5) {
|
||
sb.append("定位失败,无法获取有效定位依据,请检查运营商网络或者Wi-Fi网络是否正常开启,尝试重新请求定位");
|
||
sb.append(diagnosticMessage);
|
||
} else if (diagnosticType == 6) {
|
||
sb.append("定位失败,无法获取有效定位依据,请尝试插入一张sim卡或打开Wi-Fi重试");
|
||
sb.append("\n" + diagnosticMessage);
|
||
} else if (diagnosticType == 7) {
|
||
sb.append("定位失败,飞行模式下无法获取有效定位依据,请关闭飞行模式重试");
|
||
sb.append("\n" + diagnosticMessage);
|
||
} else if (diagnosticType == 9) {
|
||
sb.append("定位失败,无法获取任何有效定位依据");
|
||
sb.append("\n" + diagnosticMessage);
|
||
}
|
||
} else if (locType == BDLocation.TypeServerError) {
|
||
if (diagnosticType == 8) {
|
||
sb.append("定位失败,请确认您定位的开关打开状态,是否赋予APP定位权限");
|
||
sb.append("\n" + diagnosticMessage);
|
||
}
|
||
}
|
||
Log.e(TAG, "onLocationChanged: " + "定位失败");
|
||
SPUtils.put(mContext, "AmapError", sb);
|
||
Log.e(TAG, "onLocDiagnosticMessage: " + sb);
|
||
}
|
||
};
|
||
}
|