简体   繁体   English

某些手机​​的Android GPS定位请求失败

[英]Android GPS Location Request Failed for Some Phones

I am currently developing an android app using the android.location.API. 我目前正在使用android.location.API开发一个android应用。

Usually the GPS location request succeeded in some phones(Galaxy Note2) but failed(getLastKnownLocation() returns null) in others(Galaxy S3 and S4). 通常,GPS定位请求在某些手机(Galaxy Note2)中成功,但是在其他手机(Galaxy S3和S4)中失败(getLastKnownLocation()返回null)。

It was weird that the GPS worked for S3 & S4 if I first activated the Samsung built-in function "GPS Satellite View" which will scan the satellites available. 如果我首先激活了三星内置功能“ GPS卫星视图”,它将扫描可用的卫星,那么GPS在S3和S4上工作是很奇怪的。

My guess is that both S3 & S4 are using Mediatek chips which provide poor GPS signals. 我的猜测是S3和S4都使用联发科芯片,它们提供的GPS信号较差。 So does anyone know how to perform the same function like scanning satellites in android? 那么,有谁知道如何执行相同的功能,例如在android中扫描卫星?

Note that my app is used in China so please don't suggest any Google related services. 请注意,我的应用程序在中国使用,因此请不要建议任何与Google相关的服务。

Thank you. 谢谢。

public class MyLocationManager implements android.location.LocationListener{

    private static MyLocationManager instance = null;
    private Location currentLocation;
    private String provider;
    private LocationManager locationManager;
    private Context context;

    public static MyLocationManager getInstance(){
        if (instance == null){
            instance = new MyLocationManager();
        }
        return instance;
    }

    public boolean hasLocation(){
        if(currentLocation == null){
            return false;
        } else{
            return true;
        }
    }

    public float distanceInMetersFromLocation(double latitude, double longitude){
        if (currentLocation == null){
            return -1;
        }
        float[] results = new float[3];
        Location.distanceBetween(currentLocation.getLatitude(), currentLocation.getLongitude(), latitude, longitude, results);

        return results[0];
    }


    public void startListenLocation(Context context) {

        this.context = context;

        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean wifiEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (gpsEnabled){
            provider = LocationManager.GPS_PROVIDER;
            Log.e("MyLocationManager", "GPS");
        } else if (wifiEnabled){
            provider = LocationManager.NETWORK_PROVIDER;
            Log.e("MyLocationManager", "NetWork");
        } else {
            Log.e("MyLocationManager", "Please Enable Location Service");
            return;
        }

        requestLocationUpdates();

        if(provider.equals(LocationManager.GPS_PROVIDER)){
            currentLocation = locationManager.getLastKnownLocation(provider);
        } else{
            currentLocation = locationManager.getLastKnownLocation(provider);
        }

        if(currentLocation == null && provider.equals(LocationManager.GPS_PROVIDER)){
            Log.e("MyLocationManager", "GPS Failed");
            if(wifiEnabled){
                Log.e("MyLocationManager", "Use Wifi");
                provider = LocationManager.NETWORK_PROVIDER;
                requestLocationUpdates();
                currentLocation = locationManager.getLastKnownLocation(provider);
            }else{
                return;
            }
        }

    }

    public void stopListenLocation(){
        //stop updating after retrieving the location
        locationManager.removeUpdates(this);
    }

    public void requestLocationUpdates(){
        if(provider != null && !provider.isEmpty()){
            locationManager.requestLocationUpdates(provider, 60000, 10, this);
        } else{
            Log.e("MyLocationManager", "Request Location Updates Failed: Provider is null.");
        }
    }


    @Override
    public void onLocationChanged(Location location) {
        currentLocation = location;
        Log.d("SONIC", "location changed to "+location);

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }

}

Actually this is a device issue. 实际上,这是设备问题。

Some devices doesn't support getLastKnownLocation (eg Motorola XT720). 某些设备不支持getLastKnownLocation (例如Motorola XT720)。 You have to implement LocationListener and save location updates in onLocationChanged by yourself to database or sharedpreferences . 您必须实现LocationListener并将位置更新由您自己保存在onLocationChanged到数据库或sharedpreferences And when getLastKnownLocation is null use that saved location. getLastKnownLocation为null时,使用该保存的位置。

You can go with Best Provider approach to get the fast location as much as possible. 您可以使用“最佳提供者”方法来尽可能快地获得位置。

Read this blog hope it helps you. 阅读博客,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM