Location is not getting in samsung phone if GPS is disabled

。_饼干妹妹 提交于 2019-12-20 05:15:20

问题


I use samsung phone to get location through LocationManager API, i'm unable to get location if GPS is disabled, through network provider i'm unable to get the location.

Here is the code, this works well in HTC & sony even disabling GPS but not in Samsung phone.

public Location getLocation() {
        try {
            mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

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

            // getting network status
            isNetworkEnabled = mLocationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER); //fails in samsung phone returns NULL

            if (isGPSEnabled == false && isNetworkEnabled == false) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    mLocationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    if (mLocationManager != null) {
                        mLocation = mLocationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (mLocation == null) {
                        mLocationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        if (mLocationManager != null) {
                            mLocation = mLocationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return mLocation;
    }

回答1:


This behavior is unique to Samsung Phones. HTC you wont face this issue.

You have to kick start locationManager, since by default it looks into the google maps cache and does not care about how stale that cache is for the current location information. Do this, launch your application, form a different gps location and notice that it still shows the old location. Now launch google maps, and relaunch your app, your location would have changed. To programmatically kick start your app for "current location" run the following code before you call getLastKnownLocation

YOUR_APPLICATION_CONTEXT.getLocationManager().requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    @Override
    public void onProviderEnabled(String provider) {
    }
    @Override
    public void onProviderDisabled(String provider) {
    }
    @Override
    public void onLocationChanged(final Location location) {
    }
});


来源:https://stackoverflow.com/questions/17761390/location-is-not-getting-in-samsung-phone-if-gps-is-disabled

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