Get GPS Status on location changed

坚强是说给别人听的谎言 提交于 2019-11-28 01:36:10
CRUSADER

Use NETWORK_PROVIDER instead to get updated location all time. like this..

public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(Context.LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
//                  locationManager.requestLocationUpdates(
//                          LocationManager.NETWORK_PROVIDER,
//                          MIN_TIME_BW_UPDATES,
//                          MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled){
                    if (location == null){
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
//                      locationManager.requestLocationUpdates(
//                              LocationManager.GPS_PROVIDER,
//                              MIN_TIME_BW_UPDATES,
//                              MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }
//          if (!isGPSEnabled) {
//              showSettingsAlert();
//          } 
        } catch (Exception e){
            e.printStackTrace();
        }

        return location;
    }

Hope this helps

It sounds like you want to get the current location, and you want to know when a current location is unknown, so you do not try to use an old location. In that case, you can try to get the current location, and then test if it is old or not.

long fresh=60000; // 1 minute in milliseconds
if(System.currentTimeMillis()-location.getTime()>fresh)
    // Do something useful with location

If you could not get the location using GPS, you may need to get the location using NETWORK PROVIDER and compare with you last location fix to know which one is better and use accordingly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!