feat: 增加原生定位
This commit is contained in:
parent
c56322abb9
commit
7299e1e993
@ -77,6 +77,7 @@ dependencies {
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||
implementation "androidx.lifecycle:lifecycle-common:2.4.1"
|
||||
implementation "androidx.lifecycle:lifecycle-process:2.4.1"
|
||||
//友盟自动检测bug
|
||||
implementation 'com.umeng.umsdk:apm:1.2.0'
|
||||
//友盟基础库依赖(必须)
|
||||
|
@ -31,6 +31,7 @@ import com.navinfo.outdoor.fragment.TreasureFragment;
|
||||
import com.navinfo.outdoor.http.HttpInterface;
|
||||
import com.navinfo.outdoor.room.PoiDatabase;
|
||||
import com.navinfo.outdoor.util.BackHandlerHelper;
|
||||
import com.navinfo.outdoor.util.LocationLifeCycle;
|
||||
import com.navinfo.outdoor.util.NaviUtils;
|
||||
import com.navinfo.outdoor.util.NoSlideViewPager;
|
||||
|
||||
@ -38,6 +39,7 @@ import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentPagerAdapter;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.viewpager.widget.ViewPager;
|
||||
|
||||
import android.content.Context;
|
||||
@ -102,6 +104,7 @@ public class HomeActivity extends BaseActivity {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
private LocationLifeCycle locationLifeCycle;
|
||||
|
||||
@Override
|
||||
protected int getLayout() {
|
||||
@ -113,6 +116,9 @@ public class HomeActivity extends BaseActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
DialogSettings.style = DialogSettings.STYLE.STYLE_IOS;
|
||||
DialogSettings.init();
|
||||
locationLifeCycle = new LocationLifeCycle(this);
|
||||
// 注册位置更新的lifeCycle
|
||||
getLifecycle().addObserver(locationLifeCycle);
|
||||
if (Constant.USHERED != null) {
|
||||
Constant.initRootFolder(Constant.USHERED);
|
||||
// 检测用户是否为虚拟定位
|
||||
@ -558,5 +564,7 @@ public class HomeActivity extends BaseActivity {
|
||||
PoiDatabase.destroy();
|
||||
// 清空登录信息
|
||||
Constant.clearLoginInfo();
|
||||
// 注册位置更新的lifeCycle
|
||||
getLifecycle().removeObserver(locationLifeCycle);
|
||||
}
|
||||
}
|
@ -141,7 +141,6 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
|
||||
private StringBuilder picturesBuilder;
|
||||
private LatLng startLatLine, endLatLine;
|
||||
private TextView tvTitle, tvConvert/*照片已转换为webp的张数*/;
|
||||
private GPSUtils gpsUtils;
|
||||
private Location gpsLocation;
|
||||
private boolean isMapSlide = false;
|
||||
private boolean isOration = true; // 当前是否为自动拍照模式
|
||||
@ -230,7 +229,6 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
|
||||
ivPicVideoImage = findViewById(R.id.iv_pic_video);
|
||||
ImageView btnSwitch = findViewById(R.id.btn_switch);
|
||||
btnSwitch.setOnClickListener(this);
|
||||
gpsUtils = new GPSUtils(PicturesActivity.this);
|
||||
gpsUtils.setOnClickGPSStatus(new GPSUtils.OnClickGPSStatus() {
|
||||
@Override
|
||||
public void onGpsCount(int count) {
|
||||
|
@ -10,6 +10,7 @@ import android.util.Log;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import androidx.lifecycle.ProcessLifecycleOwner;
|
||||
|
||||
import com.elvishew.xlog.LogLevel;
|
||||
import com.elvishew.xlog.XLog;
|
||||
@ -27,6 +28,7 @@ import com.navinfo.outdoor.activity.HomeActivity;
|
||||
import com.navinfo.outdoor.activity.WebActivity;
|
||||
import com.navinfo.outdoor.bean.NotificationBean;
|
||||
import com.navinfo.outdoor.http.HttpInterface;
|
||||
import com.navinfo.outdoor.util.LocationLifeCycle;
|
||||
import com.navinfo.outdoor.util.TalentLocationUtils;
|
||||
import com.tencent.map.navi.TencentNavi;
|
||||
import com.tencent.navi.surport.utils.DeviceUtils;
|
||||
@ -60,7 +62,6 @@ public class UserApplication extends Application {
|
||||
userApplication = this;
|
||||
initUMSDK();
|
||||
initOkGo();
|
||||
TalentLocationUtils.getInstance(this).startLocation(this);
|
||||
//创建一个线程池
|
||||
fixedThreadPool = Executors.newSingleThreadExecutor(); // 使用单线程线程池
|
||||
// 初始化腾讯导航
|
||||
|
@ -36,7 +36,16 @@ public class GPSUtils {
|
||||
|
||||
private Activity mContext;
|
||||
|
||||
public GPSUtils(Activity context) {
|
||||
private static GPSUtils instance;
|
||||
|
||||
public static GPSUtils getInstance(Activity mContext) {
|
||||
if (instance == null) {
|
||||
instance = new GPSUtils(mContext);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private GPSUtils(Activity context) {
|
||||
this.mContext = context;
|
||||
mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
||||
|
||||
@ -56,13 +65,15 @@ public class GPSUtils {
|
||||
mLocation = mLocationManager.getLastKnownLocation(bestProvider);
|
||||
// 监听状态
|
||||
mLocationManager.registerGnssStatusCallback(listener);
|
||||
}
|
||||
|
||||
public void startLocation() {
|
||||
// 绑定监听,有4个参数
|
||||
// 参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种
|
||||
// 参数2,位置信息更新周期,单位毫秒
|
||||
// 参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息
|
||||
// 参数4,监听
|
||||
// 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新
|
||||
|
||||
// 1秒更新一次,或最小位移变化超过1米更新一次;
|
||||
// 注意:此处更新准确度非常低,推荐在service里面启动一个Thread,在run中sleep(10000);然后执行handler.sendMessage(),更新位置
|
||||
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);
|
||||
|
@ -0,0 +1,31 @@
|
||||
package com.navinfo.outdoor.util;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.DefaultLifecycleObserver;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
|
||||
public class LocationLifeCycle implements DefaultLifecycleObserver {
|
||||
private Activity mContext;
|
||||
|
||||
public LocationLifeCycle(Activity mContext) {
|
||||
this.mContext = mContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@NonNull LifecycleOwner owner) {
|
||||
DefaultLifecycleObserver.super.onCreate(owner);
|
||||
// 开启腾讯定位
|
||||
TalentLocationUtils.getInstance(mContext).startLocation(mContext);
|
||||
// 开启原生定位
|
||||
GPSUtils.getInstance(mContext).startLocation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy(@NonNull LifecycleOwner owner) {
|
||||
DefaultLifecycleObserver.super.onDestroy(owner);
|
||||
GPSUtils.getInstance(mContext).unRegisterAllListener();
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.navinfo.outdoor.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.location.Location;
|
||||
|
||||
import com.tencent.map.geolocation.TencentLocation;
|
||||
import com.tencent.tencentmap.mapsdk.maps.LocationSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MyLocationObserve{
|
||||
public MyLocationObserve instance;
|
||||
private LOCATION_FROM currentLocationFrom = LOCATION_FROM.ORIGIN; // 当前正在启用的定位系统
|
||||
private List<LocationSource.OnLocationChangedListener> locationChangedListenerList;
|
||||
private Location currentUseLocation; // 当前正在使用的location信息
|
||||
|
||||
public MyLocationObserve() {
|
||||
locationChangedListenerList = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void init(Context mContext) {
|
||||
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
|
||||
public MyLocationObserve getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new MyLocationObserve();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public enum LOCATION_FROM {
|
||||
ORIGIN, TENCENT, BAIDU, AMAP
|
||||
}
|
||||
|
||||
public void onLocationChangeListener(LOCATION_FROM from, Location location) {
|
||||
if (from == currentLocationFrom) {
|
||||
if (currentUseLocation == null) {
|
||||
currentUseLocation = location;
|
||||
} else {
|
||||
currentUseLocation.set(location);
|
||||
}
|
||||
for (LocationSource.OnLocationChangedListener locationChangedListener: locationChangedListenerList
|
||||
) {
|
||||
locationChangedListener.onLocationChanged(currentUseLocation);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ import android.os.Message;
|
||||
import android.util.Log;
|
||||
|
||||
import com.navinfo.outdoor.api.Constant;
|
||||
import com.tencent.map.fusionlocation.observer.TencentGeoLocationObserver;
|
||||
import com.tencent.map.geolocation.TencentLocation;
|
||||
import com.tencent.map.geolocation.TencentLocationListener;
|
||||
import com.tencent.map.geolocation.TencentLocationManager;
|
||||
|
@ -1,246 +0,0 @@
|
||||
package com.navinfo.outdoor.util;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* 项目名称:ToastUtil
|
||||
* 创建人:mwb
|
||||
* 创建时间:2018/9/3 9:25
|
||||
*/
|
||||
public class ToastUtil {
|
||||
private static boolean isShow = true;//默认显示
|
||||
private static Toast mToast = null;//全局唯一的Toast
|
||||
|
||||
/*private控制不应该被实例化*/
|
||||
private ToastUtil() {
|
||||
throw new UnsupportedOperationException("不能被实例化");
|
||||
}
|
||||
|
||||
/**
|
||||
* 全局控制是否显示Toast
|
||||
*/
|
||||
public static void controlShow(boolean isShowToast) {
|
||||
isShow = isShowToast;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消Toast显示
|
||||
*/
|
||||
public void cancelToast() {
|
||||
if (isShow && mToast != null) {
|
||||
mToast.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 短时间显示Toast
|
||||
*/
|
||||
@SuppressLint("ShowToast")
|
||||
public static void showShort(Context context, CharSequence message) {
|
||||
if (isShow) {
|
||||
if (mToast == null) {
|
||||
mToast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
|
||||
} else {
|
||||
mToast.setText(message);
|
||||
}
|
||||
mToast.show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 短时间显示Toast
|
||||
* @param resId 资源ID:getResources().getString(R.string);
|
||||
*/
|
||||
@SuppressLint("ShowToast")
|
||||
public static void showShort(Context context, int resId) {
|
||||
if (isShow) {
|
||||
if (mToast == null) {
|
||||
mToast = Toast.makeText(context, resId, Toast.LENGTH_SHORT);
|
||||
} else {
|
||||
mToast.setText(resId);
|
||||
}
|
||||
mToast.show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 长时间显示Toast
|
||||
*/
|
||||
@SuppressLint("ShowToast")
|
||||
public static void showLong(Context context, CharSequence message) {
|
||||
if (isShow) {
|
||||
if (mToast == null) {
|
||||
mToast = Toast.makeText(context, message, Toast.LENGTH_LONG);
|
||||
} else {
|
||||
mToast.setText(message);
|
||||
}
|
||||
mToast.show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 长时间显示Toast
|
||||
* @param resId 资源ID:getResources().getString(R.string);
|
||||
*/
|
||||
@SuppressLint("ShowToast")
|
||||
public static void showLong(Context context, int resId) {
|
||||
if (isShow) {
|
||||
if (mToast == null) {
|
||||
mToast = Toast.makeText(context, resId, Toast.LENGTH_LONG);
|
||||
} else {
|
||||
mToast.setText(resId);
|
||||
}
|
||||
mToast.show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义显示Toast时间
|
||||
* @param duration 单位:毫秒
|
||||
*/
|
||||
@SuppressLint("ShowToast")
|
||||
public static void show(Context context, CharSequence message, int duration) {
|
||||
if (isShow) {
|
||||
if (mToast == null) {
|
||||
mToast = Toast.makeText(context, message, duration);
|
||||
} else {
|
||||
mToast.setText(message);
|
||||
}
|
||||
mToast.show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义显示Toast时间
|
||||
* @param resId 资源ID:getResources().getString(R.string);
|
||||
* @param duration 单位:毫秒
|
||||
*/
|
||||
@SuppressLint("ShowToast")
|
||||
public static void show(Context context, int resId, int duration) {
|
||||
if (isShow) {
|
||||
if (mToast == null) {
|
||||
mToast = Toast.makeText(context, resId, duration);
|
||||
} else {
|
||||
mToast.setText(resId);
|
||||
}
|
||||
mToast.show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义Toast的View
|
||||
* @param duration 单位:毫秒
|
||||
* @param view 显示自己的View
|
||||
*/
|
||||
@SuppressLint("ShowToast")
|
||||
public static void customToastView(Context context, CharSequence message, int duration, View view) {
|
||||
if (isShow) {
|
||||
if (mToast == null) {
|
||||
mToast = Toast.makeText(context, message, duration);
|
||||
} else {
|
||||
mToast.setText(message);
|
||||
}
|
||||
if (view != null) {
|
||||
mToast.setView(view);
|
||||
}
|
||||
mToast.show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义Toast的位置
|
||||
* @param duration 单位:毫秒
|
||||
*/
|
||||
@SuppressLint("ShowToast")
|
||||
public static void customToastGravity(Context context, CharSequence message, int duration, int gravity, int xOffset, int yOffset) {
|
||||
if (isShow) {
|
||||
if (mToast == null) {
|
||||
mToast = Toast.makeText(context, message, duration);
|
||||
} else {
|
||||
mToast.setText(message);
|
||||
}
|
||||
mToast.setGravity(gravity, xOffset, yOffset);
|
||||
mToast.show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义带图片和文字的Toast,最终的效果就是上面是图片,下面是文字
|
||||
* @param iconResId 图片的资源id,如:R.drawable.icon
|
||||
*/
|
||||
@SuppressLint("ShowToast")
|
||||
public static void showToastWithImageAndText(Context context, CharSequence message, int iconResId, int duration, int gravity, int xOffset, int yOffset) {
|
||||
if (isShow) {
|
||||
if (mToast == null) {
|
||||
mToast = Toast.makeText(context, message, duration);
|
||||
} else {
|
||||
mToast.setText(message);
|
||||
}
|
||||
mToast.setGravity(gravity, xOffset, yOffset);
|
||||
LinearLayout toastView = (LinearLayout) mToast.getView();
|
||||
ImageView imageView = new ImageView(context);
|
||||
imageView.setImageResource(iconResId);
|
||||
toastView.addView(imageView, 0);
|
||||
mToast.show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义Toast,针对类型CharSequence
|
||||
* @param isGravity true,表示后面的三个布局参数生效,false,表示不生效
|
||||
* @param isMargin true,表示后面的两个参数生效,false,表示不生效
|
||||
*/
|
||||
@SuppressLint("ShowToast")
|
||||
public static void customToastAll(Context context, CharSequence message, int duration, View view, boolean isGravity, int gravity, int xOffset, int yOffset, boolean isMargin, float horizontalMargin, float verticalMargin) {
|
||||
if (isShow) {
|
||||
if (mToast == null) {
|
||||
mToast = Toast.makeText(context, message, duration);
|
||||
} else {
|
||||
mToast.setText(message);
|
||||
}
|
||||
if (view != null) {
|
||||
mToast.setView(view);
|
||||
}
|
||||
if (isMargin) {
|
||||
mToast.setMargin(horizontalMargin, verticalMargin);
|
||||
}
|
||||
if (isGravity) {
|
||||
mToast.setGravity(gravity, xOffset, yOffset);
|
||||
}
|
||||
mToast.show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义Toast,针对类型resId
|
||||
* @param view :应该是一个布局,布局中包含了自己设置好的内容
|
||||
* @param isGravity true,表示后面的三个布局参数生效,false,表示不生效
|
||||
* @param isMargin true,表示后面的两个参数生效,false,表示不生效
|
||||
*/
|
||||
@SuppressLint("ShowToast")
|
||||
public static void customToastAll(Context context, int resId, int duration, View view, boolean isGravity, int gravity, int xOffset, int yOffset, boolean isMargin, float horizontalMargin, float verticalMargin) {
|
||||
if (isShow) {
|
||||
if (mToast == null) {
|
||||
mToast = Toast.makeText(context, resId, duration);
|
||||
} else {
|
||||
mToast.setText(resId);
|
||||
}
|
||||
if (view != null) {
|
||||
mToast.setView(view);
|
||||
}
|
||||
if (isMargin) {
|
||||
mToast.setMargin(horizontalMargin, verticalMargin);
|
||||
}
|
||||
if (isGravity) {
|
||||
mToast.setGravity(gravity, xOffset, yOffset);
|
||||
}
|
||||
mToast.show();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user