如果 GPS 被禁用,位置将无法进入三星手机

Location is not getting in samsung phone if GPS is disabled(如果 GPS 被禁用,位置将无法进入三星手机)

本文介绍了如果 GPS 被禁用,位置将无法进入三星手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用三星手机通过 LocationManager API 获取位置,如果禁用 GPS,我无法获取位置,通过网络提供商我无法获取位置.

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.

这是代码,这在 HTC &索尼甚至禁用了 GPS,但三星手机没有.

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;
    }

推荐答案

这种行为是三星手机独有的.HTC你不会遇到这个问题.

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

您必须启动 locationManager,因为默认情况下它会查看 google maps 缓存并且不关心缓存对于当前位置信息的陈旧程度.执行此操作,启动您的应用程序,形成不同的 gps 位置并注意它仍然显示旧位置.现在启动谷歌地图,然后重新启动你的应用程序,你的位置就会改变.要以编程方式启动您的应用程序以获取当前位置",请在调用 getLastKnownLocation

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) {
    }
});

这篇关于如果 GPS 被禁用,位置将无法进入三星手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如果 GPS 被禁用,位置将无法进入三星手机

基础教程推荐