簡體   English   中英

Android Google Play服務GPS無法發送位置

[英]Android Google Play Services GPS not sending location

我制作了一個可以跟蹤用戶位置的應用程序,但有時GPS似乎無法發送該位置。

而且似乎GPS並未斷開連接,因為我已經為此輸入了日志,並且沒有任何日志表明GPS斷開連接。

看起來好像它停止了一段時間的onLocationChanged調用,然后再次起作用。

這是問題的一個例子。 如您所見,我正在穿過建築物..

在網上進行了一些研究之后,我什至還設置了部分喚醒鎖,但這似乎無效。

在此處輸入圖片說明

因此,我要做的是啟動應用程序,然后按手機上的電源按鈕以關閉屏幕並開始駕駛。 到達目的地后,我打開應用程序並停止我的應用程序。

並非總是如此。

有什么可能的原因的想法嗎?

謝謝,

這是代碼:

public class LocationService extends Service implements
ConnectionCallbacks,
OnConnectionFailedListener,
LocationListener {

    private static Logger LOGGER = LoggerFactory.getLogger(LocationService.class);

    private final IBinder _binder = new LocalBinder();

    private LocationClient _locationClient;
    private LocationRequest REQUEST = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(5000)
            .setFastestInterval(1000)
            .setSmallestDisplacement(5);

    public class LocalBinder extends Binder {
        public LocationService getService() {
            return LocationService.this;
        }
    }

    private PowerManager.WakeLock wakeLock;

    @Override
    public void onCreate() {
        super.onCreate();

        LOGGER.debug("onCreate");

        PowerManager mgr = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
        wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Wake Lock");
    }

    public int onStartCommand (Intent intent, int flags, int startId)
    {
        super.onStartCommand(intent, flags, startId);

        LOGGER.debug("onStartCommand");

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return _binder;
    }

    @Override
    public void onDestroy(){
        super.onDestroy(); 

        LOGGER.debug("onDestroy");

        if(_locationClient != null) {
            stopAcquiringLocation();
            _locationClient = null;
        }

        if(wakeLock.isHeld())
            wakeLock.release();
    }


    @Override
    public void onLocationChanged(Location location) {
        LOGGER.debug("location: "+location.getLatitude()+","+location.getLongitude() +" Accurracy: "+location.getAccuracy() + " Time:"+new Date(location.getTime()));

        if(location.getAccuracy() <= 15) {
            //Save the location
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        LOGGER.info("GPS Connected");

        // Request location updates using static settings
        getLocationClient().requestLocationUpdates(REQUEST, this);
    }

    @Override
    public void onDisconnected() {
        LOGGER.info("GPS Disconnected");

        if(_locationClient != null) {
            stopAcquiringLocation();
            _locationClient = null;
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        LOGGER.info("GPS ConnectionFailed "+connectionResult.toString());
    }



    private LocationClient getLocationClient()
    {
        if(_locationClient == null)
            _locationClient = new LocationClient(this, this, this);

        return _locationClient;
    }

    public void startAcquiringLocation() {
        if(Util.IsGooglePlayServiceAvailable(this)) {
            if (!getLocationClient().isConnected() || !getLocationClient().isConnecting()) {
                LOGGER.debug("getLocationClient().connect()");

                getLocationClient().connect();

                if(wakeLock.isHeld() == false)
                    wakeLock.acquire();
            }
        }
        else {
            LOGGER.warn("NO Google Play Services Available");
        }
    }

    public void stopAcquiringLocation() {
        if(Util.IsGooglePlayServiceAvailable(this)) {
            if (getLocationClient().isConnected() || getLocationClient().isConnecting()) {
                getLocationClient().removeLocationUpdates(this);
                getLocationClient().disconnect();

                LOGGER.debug("getLocationClient().disconnect()");

                if(wakeLock.isHeld())
                    wakeLock.release();
            }
        }
    }
}

沒有一些代碼很難說什么。 我會嘗試做的第一件事-因為您已經在外面測試您的應用程序-正在使用另一個類似的應用程序,然后查看您的應用程序是否存在問題,或者內置GPS是否存在硬件問題。

MIN_DISTANCE_FOR_UPDATESMIN_TIME_BW_UPDATES的值是否合理?

手機中啟用的節能模式也可能導致GPS位置更新的頻率降低。

我認為,對於更多建議,需要一些代碼。

另外,我建議嘗試在建築物較低的區域(1-2層)進行測試,以免GPS本身出現“信號丟失”的情況。 嘗試記錄所有gps事件,並在日志中查看正在發生的情況。

同樣在GPS中,您的精度至少為10 m。 通常,由於有意使用數據中的限制,您可以獲得更好的位置。

丹尼爾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM