• LocationUtil


    package com.android.demo.lileidemo.utils;

    import android.Manifest;
    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.ContextCompat;
    import android.util.Log;

    import com.android.demo.lileidemo.MyApplication;
    import com.android.demo.lileidemo.constant.AppConstants;

    import java.util.List;

    /**
    * @Description: LocationUtil.
    * @Author: lilei.
    * @CreateDate: 2/19/2020$ 10:03 AM$.
    * @Version: 1.0.
    */
    public class LocationUtil {
    private static final String TAG = AppConstants.APP_TAG + "LocationUtil ";
    private static LocationUtil mInstance;
    private LocationManager mLocationManager;
    private String mLocationProvider;
    private Context mContext;

    public LocationUtil() {
    mContext = MyApplication.getAppContext();
    }

    public static LocationUtil getInstance() {
    if (mInstance == null) {
    mInstance = new LocationUtil();
    }
    return mInstance;
    }

    public boolean checkLocationPermission() {
    if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
    || ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    return false;
    } else {
    return true;
    }

    }

    public boolean hasGPSDevice(Context context) {
    final LocationManager mgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    if (mgr == null) {
    return false;
    }
    final List<String> providers = mgr.getAllProviders();
    if (providers == null) {
    return false;
    }
    return providers.contains(LocationManager.GPS_PROVIDER);
    }

    /**
    * Get Location.
    * Should call requestLocationUpdates first.
    *
    * @return location.
    */
    public Location getLocation() {
    if (!checkLocationPermission()) {
    Log.d(TAG, "getLocation please set location permission first!!");
    return null;
    }
    if (null == mLocationManager) {
    requestLocationUpdates();
    return null;
    }
    //Get location.
    Location location = mLocationManager.getLastKnownLocation(mLocationProvider);
    if (location != null) {
    Log.d(TAG, "getLocation() getLongitude:" + location.getLongitude()
    + " getLatitude:" + location.getLatitude());
    } else {
    Log.d(TAG, "getLocation location is null call requestLocationUpdates!!");
    requestLocationUpdates();
    }
    return location;

    }

    /**
    * Request location updates.
    * after call this function can call getLocation.
    * after call getLocation should call removeLocationUpdates to release monitoring.
    */
    public void requestLocationUpdates() {
    Log.d(TAG, "requestLocationUpdates begin");
    //Get permission (if you do not have permission to open,
    //a dialog box will pop up asking whether to open permission).
    if (!checkLocationPermission()) {
    Log.d(TAG, "requestLocationUpdates please set location permission first!!");
    return;
    }

    //Get geolocation Manager.
    if (null == mLocationManager) {
    mLocationManager = (LocationManager) mContext.getSystemService(
    Context.LOCATION_SERVICE);
    }
    //Get all available location providers.
    List<String> providers = mLocationManager.getProviders(true);
    if (providers.contains(LocationManager.GPS_PROVIDER)) {
    //If it's GPS.
    mLocationProvider = LocationManager.GPS_PROVIDER;
    } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
    //If it's network.
    mLocationProvider = LocationManager.NETWORK_PROVIDER;
    } else {
    Log.e(TAG, "No location provider available!");
    return;
    }

    //Monitoring geographic location changes.
    mLocationManager.requestLocationUpdates(mLocationProvider, 1000, 1, mLocationListener);
    }

    public void removeLocationUpdates() {
    Log.d(TAG, "removeLocationUpdates()");
    if (null != mLocationManager) {
    mLocationManager.removeUpdates(mLocationListener);
    }
    }

    LocationListener mLocationListener = new LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle arg2) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onLocationChanged(Location location) {
    //如果位置发生变化,重新显示 如果位置改变,经纬度没有变,不会执行此函数 应该说经纬度发生变化执行此函数
    Log.d(TAG, "mLocationListener.onLocationChanged 经度" + location.getLongitude()
    + "纬度" + location.getLatitude());
    }
    };

    }
  • 相关阅读:
    js精度丢失问题处理
    button居中
    js存储 cookie,localStorage,sessionStorage的比较
    js 常用的DOM,BOM操作
    js事件代理理解
    oneplus前端开发面试
    instanceof
    js原型和原型链
    js构造函数
    NC 6.X笔记(编辑中)
  • 原文地址:https://www.cnblogs.com/adamli/p/13140805.html
Copyright © 2020-2023  润新知