位置是没有得到三星手机,如果GPS被禁用三星手机、位置、GPS

2023-09-08 10:18:28 作者:激浊扬清

我用三星手机通过LocationManager API来获取位置,我无法得到位置,如果GPS是残疾人,可以通过网络提供我无法得到的位置。

下面是code,这非常适用于HTC和放大器;索尼甚至停用GPS,但不是在三星手机。

 公共场所的getLocation(){
        尝试 {
            mLocationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

            //获取GPS状态
            isGPSEnabled = mLocationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            //获取网络状态
            isNetworkEnabled = mLocationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER); //失败的三星手机返回NULL

            如果(isGPSEnabled ==假放;&安培; isNetworkEnabled == FALSE){
                //没有网络提供商启用
            } 其他 {
                this.canGetLocation = TRUE;
                如果(isNetworkEnabled){
                    mLocationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
                    如果(mLocationManager!= NULL){
                        mLocation = mLocationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    }
                }
                //如果GPS功能的获得纬度/经度使用GPS服务
                如果(isGPSEnabled){
                    如果(mLocation == NULL){
                        mLocationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
                        如果(mLocationManager!= NULL){
                            mLocation = mLocationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        }
                    }
                }
            }

        }赶上(例外五){
            e.printStackTrace();
        }

        返回mLocation;
    }
 

解决方案 三星手机诡异Bug 进入2021年 电池统计不见了

此行​​为是唯一的三星手机。 HTC你不会遇到这个问题。

您必须开始踢locationManager,因为默认情况下它看起来到谷歌地图缓存和不关心如何过时的缓存是当前的位置信息。为此,启动应用程序,形成不同的GPS位置,请注意,仍显示原来的位置。现在推出谷歌地图,并重新启动您的应用程序,你的位置会改变。要以编程方式开始踢你的应用程序的当前位置运行以下code致电前 getLastKnownLocation

  YO​​UR_APPLICATION_CONTEXT.getLocationManager()。requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,0,0,新LocationListener的(){
    @覆盖
    公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
    }
    @覆盖
    公共无效onProviderEnabled(字符串提供商){
    }
    @覆盖
    公共无效onProviderDisabled(字符串提供商){
    }
    @覆盖
    公共无效onLocationChanged(最终位置定位){
    }
});
 

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

解决方案

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

 
精彩推荐
图片推荐