diff --git a/app/build.gradle b/app/build.gradle index e7ca139..388c8e1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -72,8 +72,8 @@ android { //新平台正式 newly { flavorDimensions "default" - versionCode 20 - versionName "2.9" + versionCode 21 + versionName "3.0" buildConfigField "String", "platform", '"ZhanRui"' manifestPlaceholders = [ AMAP_KEY: "6e339305206eeb43f2b4cc1f31751f89" @@ -295,7 +295,9 @@ dependencies { // implementation 'cn.jiguang.sdk:jpush:4.3.0' // 此处以JPush 3.5.6 版本为例。 // implementation 'cn.jiguang.sdk:jcore:2.9.0' // 此处以JCore 2.2.8 版本为例。 //高德地图定位 - implementation 'com.amap.api:location:5.1.0' +// implementation 'com.amap.api:location:5.1.0' + //百度地图 + implementation 'com.baidu.lbsyun:BaiduMapSDK_Location:9.1.8' //MMKV implementation 'com.tencent:mmkv-static:1.2.13' //腾讯移动推送 TPNS diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bb461b1..9bab9a2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -66,6 +66,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -569,13 +612,13 @@ - - + + \ No newline at end of file diff --git a/app/src/main/java/com/aoleyun/sn/manager/AmapManager.java b/app/src/main/java/com/aoleyun/sn/manager/AmapManager.java index cd826fc..47e3c55 100644 --- a/app/src/main/java/com/aoleyun/sn/manager/AmapManager.java +++ b/app/src/main/java/com/aoleyun/sn/manager/AmapManager.java @@ -1,29 +1,35 @@ package com.aoleyun.sn.manager; +import android.annotation.SuppressLint; import android.content.Context; 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.aoleyun.sn.utils.SPUtils; +import com.baidu.location.BDAbstractLocationListener; +import com.baidu.location.BDLocation; +import com.baidu.location.LocationClient; +import com.baidu.location.LocationClientOption; public class AmapManager { + private String TAG = AmapManager.class.getSimpleName(); + + @SuppressLint("StaticFieldLeak") private static AmapManager sInstance; private Context mContext; - public static AMapLocationClient locationClient = null; - private String TAG = AmapManager.class.getSimpleName(); + private LocationClient mLocationClient = null; + private LocationClientOption mOption; private AmapManager(Context context) { this.mContext = context; } 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() { @@ -34,57 +40,130 @@ public class AmapManager { return sInstance; } - public AMapLocationClient getLocationClient() { - if (null == locationClient) { + public void initAmap() { + mLocationClient = new LocationClient(mContext); + mLocationClient.setLocOption(getDefaultLocationClientOption()); + mLocationClient.registerLocationListener(mListener); + mLocationClient.stop(); + mLocationClient.start(); + } + + public LocationClient getLocationClient() { + if (mLocationClient == null) { initAmap(); } - return locationClient; + return mLocationClient; } - public void initAmap() { - locationClient = new AMapLocationClient(mContext); - AMapLocationClientOption option = new AMapLocationClientOption(); - option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn); - option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); - option.setNeedAddress(true); - //获取一次定位结果: - //该方法默认为false。 - option.setOnceLocation(true); - //获取最近3s内精度最高的一次定位结果: - //设置setOnceLocationLatest(boolean b)接口为true,启动定位时SDK会返回最近3s内精度最高的一次定位结果。 - // 如果设置其为true,setOnceLocation(boolean b)接口也会被设置为true,反之不会,默认为false。 - option.setOnceLocationLatest(true); - locationClient.setLocationOption(option); - //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。 - //设置定位监听 - locationClient.setLocationListener(new AMapLocationListener() { + /*** + * + * @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.Hight_Accuracy); // 可选,默认高精度,设置定位模式,高精度,低功耗,仅设备,模糊 + mOption.setIsNeedAltitude(false); // 可选,默认false,设置定位时是否需要海拔信息,默认不需要,除基础定位版本都可用 + // 可选,设置首次定位时选择定位速度优先还是定位准确性优先,默认为速度优先 +// mOption.setFirstLocType(LocationClientOption.FirstLocType.SPEED_IN_FIRST_LOC); + } + return mOption; + } - @Override - public void onLocationChanged(AMapLocation aMapLocation) { - StringBuilder sb = new StringBuilder(); - //errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明 - if (aMapLocation.getErrorCode() == 0) { - Log.e(TAG, "onLocationChanged: " + "定位成功"); - Log.e(TAG, "onLocationChanged: " + aMapLocation.getAddress()); - sb.append(aMapLocation.getAddress()).append("\n"); - SPUtils.put(mContext, "AmapAddress", aMapLocation.getAddress()); - SPUtils.put(mContext, "longitude", aMapLocation.getLongitude()); - SPUtils.put(mContext, "latitude", aMapLocation.getLatitude()); - SPUtils.put(mContext, "AmapError", ""); - } else { - //定位失败 - sb.append("定位失败" + "\n"); - Log.e(TAG, "onLocationChanged: " + "定位失败"); - SPUtils.put(mContext, "AmapError", String.valueOf(aMapLocation.getErrorInfo())); - } - Log.e(TAG, (String) SPUtils.get(mContext, "AmapAddress", "-")); - Log.e(TAG, (String) SPUtils.get(mContext, "AmapError", "-")); - Log.e(TAG, "amap: " + sb.toString()); + + /***** + * + * 定位结果回调,重写onReceiveLocation方法,可以直接拷贝如下代码到自己工程中修改 + * + */ + private BDAbstractLocationListener mListener = new BDAbstractLocationListener() { + + /** + * 定位请求回调函数 + * @param location 定位结果 + */ + @Override + public void onReceiveLocation(BDLocation location) { + // TODO Auto-generated method stub + if (null != location && location.getLocType() != BDLocation.TypeServerError) { + Log.e(TAG, "onLocationChanged: " + "定位成功"); + Log.e(TAG, "onLocationChanged: " + location.getAddress()); + SPUtils.put(mContext, "AmapAddress", location.getAddress()); + SPUtils.put(mContext, "longitude", location.getLongitude()); + SPUtils.put(mContext, "latitude", location.getLatitude()); + SPUtils.put(mContext, "AmapError", ""); } - }); - //设置场景模式后最好调用一次stop,再调用start以保证场景模式生效 - locationClient.stopLocation(); - locationClient.startLocation(); - Log.e(TAG, "initAmap: " + "startLocation"); - } + Log.e(TAG, (String) SPUtils.get(mContext, "AmapAddress", "-")); + Log.e(TAG, (String) SPUtils.get(mContext, "AmapError", "-")); + } + + @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); +// logMsg(sb.toString(), tag); + } + }; } diff --git a/app/src/main/java/com/aoleyun/sn/service/GuardService.java b/app/src/main/java/com/aoleyun/sn/service/GuardService.java index 084220b..ce974b6 100644 --- a/app/src/main/java/com/aoleyun/sn/service/GuardService.java +++ b/app/src/main/java/com/aoleyun/sn/service/GuardService.java @@ -20,10 +20,8 @@ import androidx.annotation.Nullable; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.amap.api.location.AMapLocationClient; import com.aoleyun.sn.BuildConfig; import com.aoleyun.sn.KeepAliveConnection; -import com.aoleyun.sn.base.BaseApplication; import com.aoleyun.sn.bean.BaseResponse; import com.aoleyun.sn.comm.CommonConfig; import com.aoleyun.sn.comm.PackageNames; @@ -45,6 +43,7 @@ import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.task.DownloadTask; +import com.baidu.location.LocationClient; import java.io.IOException; import java.util.List; @@ -241,9 +240,9 @@ public class GuardService extends Service { break; case Intent.ACTION_SCREEN_ON: { long time = System.currentTimeMillis(); - AMapLocationClient aMapLocationClient = AmapManager.getInstance().getLocationClient(); - aMapLocationClient.stopLocation(); - aMapLocationClient.startLocation(); + LocationClient locationClient = AmapManager.getInstance().getLocationClient(); + locationClient.stop(); + locationClient.start(); getLockState("2", String.valueOf(time)); // sendScreenStatus(1); } diff --git a/app/src/main/java/com/aoleyun/sn/tpush/MessageReceiver.java b/app/src/main/java/com/aoleyun/sn/tpush/MessageReceiver.java index c7b1228..5a5d9fb 100644 --- a/app/src/main/java/com/aoleyun/sn/tpush/MessageReceiver.java +++ b/app/src/main/java/com/aoleyun/sn/tpush/MessageReceiver.java @@ -22,9 +22,6 @@ import android.widget.Toast; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.amap.api.location.AMapLocation; -import com.amap.api.location.AMapLocationClient; -import com.amap.api.location.AMapLocationListener; import com.aoleyun.sn.R; import com.aoleyun.sn.base.BaseApplication; import com.aoleyun.sn.bean.BaseResponse; @@ -55,6 +52,9 @@ import com.aoleyun.sn.utils.ServiceAliveUtils; import com.aoleyun.sn.utils.ToastUtil; import com.aoleyun.sn.utils.Utils; import com.arialyy.aria.core.Aria; +import com.baidu.location.BDAbstractLocationListener; +import com.baidu.location.BDLocation; +import com.baidu.location.LocationClient; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; @@ -618,36 +618,69 @@ public class MessageReceiver extends XGPushBaseReceiver { } else { Log.e(TAG, "mPresenter is NULL"); } - AMapLocationClient aMapLocationClient = AmapManager.getInstance().getLocationClient(); - aMapLocationClient.stopLocation(); - aMapLocationClient.startLocation(); - aMapLocationClient.setLocationListener(new AMapLocationListener() { + LocationClient locationClient = AmapManager.getInstance().getLocationClient(); + locationClient.stop(); + locationClient.start(); + locationClient.registerLocationListener(new BDAbstractLocationListener() { @Override - public void onLocationChanged(AMapLocation aMapLocation) { - StringBuilder sb = new StringBuilder(); - //errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明 - if (aMapLocation.getErrorCode() == 0) { + public void onReceiveLocation(BDLocation bdLocation) { + Log.e(TAG, "onReceiveLocation: "); + if (null != bdLocation && bdLocation.getLocType() != BDLocation.TypeServerError) { Log.e(TAG, "onLocationChanged: " + "定位成功"); - Log.e(TAG, "onLocationChanged: " + aMapLocation.getAddress()); - sb.append(aMapLocation.getAddress()).append("\n"); - SPUtils.put(mContext, "AmapAddress", aMapLocation.getAddress()); - SPUtils.put(mContext, "longitude", aMapLocation.getLongitude()); - SPUtils.put(mContext, "latitude", aMapLocation.getLatitude()); - } else { - //定位失败 - sb.append("定位失败" + "\n"); - SPUtils.put(mContext, "AmapError", String.valueOf(aMapLocation.getErrorCode())); + Log.e(TAG, "onLocationChanged: " + bdLocation.getAddress()); + SPUtils.put(mContext, "AmapAddress", bdLocation.getAddress()); + SPUtils.put(mContext, "longitude", bdLocation.getLongitude()); + SPUtils.put(mContext, "latitude", bdLocation.getLatitude()); + SPUtils.put(mContext, "AmapError", ""); } Log.e(TAG, (String) SPUtils.get(mContext, "AmapAddress", "-")); Log.e(TAG, (String) SPUtils.get(mContext, "AmapError", "-")); - Log.e(TAG, "amap: " + sb.toString()); - NetInterfaceManager.getInstance().updateDeviceInfo(new NetInterfaceManager.onCompleteCallback() { - @Override - public void onComplete() { + } + @Override + public void onLocDiagnosticMessage(int locType, int diagnosticType, String diagnosticMessage) { + super.onLocDiagnosticMessage(locType, diagnosticType, diagnosticMessage); + 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); } }); break;