|
|
@@ -0,0 +1,253 @@
|
|
|
+package com.mgtech.base_library.util;
|
|
|
+
|
|
|
+import android.Manifest;
|
|
|
+import android.app.Activity;
|
|
|
+import android.app.AlertDialog;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.DialogInterface;
|
|
|
+import android.content.Intent;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.graphics.Typeface;
|
|
|
+import android.location.Criteria;
|
|
|
+import android.location.Location;
|
|
|
+import android.location.LocationListener;
|
|
|
+import android.location.LocationManager;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.provider.Settings;
|
|
|
+import android.widget.Button;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+import androidx.core.app.ActivityCompat;
|
|
|
+import androidx.core.content.ContextCompat;
|
|
|
+
|
|
|
+import com.coorchice.library.utils.LogUtils;
|
|
|
+import com.mgtech.base_library.R;
|
|
|
+
|
|
|
+import pub.devrel.easypermissions.AfterPermissionGranted;
|
|
|
+import pub.devrel.easypermissions.EasyPermissions;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ProjectName: BaseLibrary
|
|
|
+ * @Package: com.mgtech.base_library.util
|
|
|
+ * @ClassName: GoogleLocationUtil
|
|
|
+ * @Description: google定位工具类
|
|
|
+ * @Author: 牛松涛
|
|
|
+ * @CreateDate: 2020/4/17 13:01
|
|
|
+ * @UpdateUser: 更新者:
|
|
|
+ * @UpdateDate: 2020/4/17 13:01
|
|
|
+ * @UpdateRemark: 更新说明:
|
|
|
+ * @Version: 1.0
|
|
|
+ */
|
|
|
+public class GoogleLocationUtil {
|
|
|
+ private Activity context;
|
|
|
+ private LocationInfoListener locationInfoListener;
|
|
|
+
|
|
|
+ private final long MIN_TIME = 10 * 60 * 1000L;
|
|
|
+ private final float MIN_DISTANCE = 2000F;
|
|
|
+ private final int REQUEST_LOCATION = 999;
|
|
|
+ private MyLocationListener myLocationListener = new MyLocationListener();
|
|
|
+ private LocationManager locationManager;
|
|
|
+
|
|
|
+
|
|
|
+ public GoogleLocationUtil(Activity context, LocationInfoListener locationInfoListener) {
|
|
|
+ this.context = context;
|
|
|
+ this.locationInfoListener = locationInfoListener;
|
|
|
+ }
|
|
|
+
|
|
|
+ public interface LocationInfoListener{
|
|
|
+ void locationSuccess(double lat, double lng);
|
|
|
+
|
|
|
+ void locationFailed(String msg);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @method startLocation
|
|
|
+ * @description 开启定位
|
|
|
+ * @date: 2020/4/17 13:46
|
|
|
+ * @author: 牛松涛
|
|
|
+ * @param
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public void startLocation(){
|
|
|
+ //开启定位,先查看权限
|
|
|
+ methodRequiresTwoPermission();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @method stopLocation
|
|
|
+ * @description 移除定位监听
|
|
|
+ * @date: 2020/4/21 9:00
|
|
|
+ * @author: 牛松涛
|
|
|
+ * @param
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public void stopLocation(){
|
|
|
+ if (locationManager !=null)
|
|
|
+ locationManager.removeUpdates(myLocationListener);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @method isLocServiceEnable
|
|
|
+ * @description 机是否开启位置服务,如果没有开启那么所有app将不能使用定位功能
|
|
|
+ * @date: 2020/4/17 13:36
|
|
|
+ * @author: 牛松涛
|
|
|
+ * @param
|
|
|
+ * @return boolean true--已开启定位功能 false--未开启定位功能
|
|
|
+ */
|
|
|
+ private boolean isLocServiceEnable() {
|
|
|
+ if (locationManager == null)
|
|
|
+ locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
|
|
+ boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
|
|
|
+ boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
|
|
|
+ return gps || network;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取位置信息
|
|
|
+ * @param locationInfoListener
|
|
|
+ */
|
|
|
+ private void getLocationInfo(LocationInfoListener locationInfoListener){
|
|
|
+ this.locationInfoListener = locationInfoListener;
|
|
|
+ if (locationManager == null)
|
|
|
+ locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
|
|
+ if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|
|
|
+ && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ // here to request the missing permissions, and then overriding
|
|
|
+ // public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
+ // int[] grantResults)
|
|
|
+ // to handle the case where the user grants the permission. See the documentation
|
|
|
+ // for ActivityCompat#requestPermissions for more details.
|
|
|
+ locationInfoListener.locationFailed(context.getString(R.string.map_permission_failure));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Criteria criteria = new Criteria();
|
|
|
+ criteria.setAccuracy(Criteria.ACCURACY_FINE);
|
|
|
+ criteria.setAltitudeRequired(false);
|
|
|
+ criteria.setBearingRequired(false);
|
|
|
+ criteria.setCostAllowed(true);
|
|
|
+ criteria.setPowerRequirement(Criteria.POWER_LOW);
|
|
|
+ String provider = locationManager.getBestProvider(criteria, true);
|
|
|
+
|
|
|
+// LogUtils.e("nst","getLocationInfo: -----" + provider);
|
|
|
+ if (provider == null){
|
|
|
+ locationInfoListener.locationFailed(context.getString(R.string.map_permission_failure));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Location location = locationManager.getLastKnownLocation(provider);
|
|
|
+ if (location != null){
|
|
|
+ LogUtil.e("nst","getLastKnownLocation:"+location.getLatitude()+"--------"+location.getLongitude());
|
|
|
+ locationInfoListener.locationSuccess(location.getLatitude(),location.getLongitude());
|
|
|
+ }else{
|
|
|
+ //如果位置移动超过200米那将会重新定位
|
|
|
+ locationManager.requestLocationUpdates(provider, MIN_TIME, MIN_DISTANCE,myLocationListener);
|
|
|
+ }
|
|
|
+
|
|
|
+// locationManager.requestLocationUpdates(provider, MIN_TIME, MIN_DISTANCE,listeners[0]);
|
|
|
+// locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE,listeners[0]);
|
|
|
+// locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE,listeners[1]);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private class MyLocationListener implements LocationListener {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onLocationChanged(Location location) {
|
|
|
+ if (locationInfoListener == null)
|
|
|
+ return;
|
|
|
+ if(location != null){
|
|
|
+ LogUtil.e("nst","onLocationChanged:"+location.getLatitude()+"--------"+location.getLongitude());
|
|
|
+ locationInfoListener.locationSuccess(location.getLatitude(),location.getLongitude());
|
|
|
+ }else{
|
|
|
+ locationInfoListener.locationFailed(context.getString(R.string.map_location_failure));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onStatusChanged(String provider, int status, Bundle extras) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onProviderEnabled(String provider) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onProviderDisabled(String provider) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //activity中要实现EasyPermissions这个方法,然后回调这个方法
|
|
|
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
+ EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @method methodRequiresTwoPermission
|
|
|
+ * @description 定位权限申请
|
|
|
+ * @date: 2020/4/17 13:35
|
|
|
+ * @author: 牛松涛
|
|
|
+ * @param
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ @AfterPermissionGranted(100)
|
|
|
+ private void methodRequiresTwoPermission() {
|
|
|
+ String[] perms = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
|
|
|
+ if (EasyPermissions.hasPermissions(context, perms)) {
|
|
|
+ // Already have permission, do the thing
|
|
|
+ //权限通过,检查是否开启定位功能
|
|
|
+ if (isLocServiceEnable())//已开启,进行定位
|
|
|
+ getLocationInfo(locationInfoListener);
|
|
|
+ else //未开启,跳转到设置进行开启
|
|
|
+ openLocationService();
|
|
|
+ } else {
|
|
|
+ // Do not have permissions, request them now
|
|
|
+ EasyPermissions.requestPermissions(context, context.getString(R.string.location_permission),
|
|
|
+ 100, perms);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
+ if (requestCode == REQUEST_LOCATION){
|
|
|
+ getLocationInfo(locationInfoListener);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void openLocationService() {
|
|
|
+ AlertDialog.Builder builder = new AlertDialog.Builder(context,R.style.Theme_AppCompat_Light_Dialog_Alert);
|
|
|
+ TextView tv = new TextView(context);
|
|
|
+ tv.setText(context.getString(R.string.location_service_turned_off));
|
|
|
+ tv.setTextSize(14);
|
|
|
+ tv.setPadding(20,20, 0, 0);
|
|
|
+ tv.setTextColor(ContextCompat.getColor(context,R.color.black_text_light));
|
|
|
+ AlertDialog alertDialog = builder
|
|
|
+ .setNegativeButton(context.getString(R.string.common_cancel), null)
|
|
|
+ .setCustomTitle(tv)
|
|
|
+ .setPositiveButton(context.getString(R.string.common_start), (dialog, which) -> {
|
|
|
+ Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
|
|
+ context.startActivityForResult(intent, REQUEST_LOCATION);
|
|
|
+ })
|
|
|
+ .show();
|
|
|
+ Button nButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
|
|
|
+ nButton.setTextColor(ContextCompat.getColor(context, R.color.orange));
|
|
|
+ nButton.setTypeface(Typeface.DEFAULT_BOLD);
|
|
|
+ Button pButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
|
|
|
+ pButton.setTextColor(ContextCompat.getColor(context, R.color.orange));
|
|
|
+ pButton.setTypeface(Typeface.DEFAULT_BOLD);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|