可靠获取当前位置

可靠获取当前位置

本文介绍了Android的 - 可靠获取当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的检查在特定时间用户是否是在给定的位置。我使用报警管理器启动,使这个呼叫服务:

My app checks at a specific time whether a user is at a given location. I use the alarm manager to start a service that makes this call:

locationManager.requestLocationUpdates(bestProvider, 0, 0, listener);

和还检查:

 locationManager.getLastKnownLocation(bestProvider);

但是,一个真正的设备上运行时,我遇到的问题。一方面, getLastKnownLocation 最有可能是最后一个地方的GPS是在,它可以在任何地方(例如,它可以从用户的当前位置英里)。所以,我就等待 requestLocationUpdates 的回调,如果它们不存在两分钟内,删除监听和放弃,对吧?

But I'm having problems when running on a real device. For one thing, getLastKnownLocation is most likely the last place the GPS was on, which could be anywhere (i.e., it could be miles from the user's current location). So I'll just wait for requestLocationUpdates callbacks, and if they aren't there within two minutes, remove the listener and give up, right?

错误,因为如果用户的位置已经稳定(即,他们使用GPS最近并没有移动),那么我的监听器永远不会被调用,因为位置不会改变。但是,GPS就可以运行,直到我的听众被删除,耗尽电池...

Wrong, because if the user's location is already stable (i.e., they've used GPS recently and haven't moved) then my listener will never be called because the location doesn't change. But the GPS will run until my listener is removed, draining the battery...

什么是正确的方式来获取当前位置,而不当前位置弄错了原来的位置?我不介意等待几分钟。

What is the right way to get the current location without mistaking an old location for the current location? I don't mind waiting a few minutes.

编辑:这可能是我错了没有被调用的监听器,它可能只是会比我还以为......很难说长一点。我倒是AP preciate一个明确的答案依旧。

It's possible that I'm wrong about the listener not being called, it may just take a little longer than I thought... Hard to say. I'd appreciate a definitive answer still.

推荐答案

在code可为这样的事情:

The code may be something like that:

public class MyLocation {
    Timer timer1;
    LocationManager lm;

    public boolean getLocation(Context context)
    {
        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        timer1=new Timer();
        timer1.schedule(new GetLastLocation(), 20000);
        return true;
    }

    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            lm.removeUpdates(this);
            //use location as it is the latest value
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    class GetLastLocation extends TimerTask {
        @Override
        public void run() {
             lm.removeUpdates(locationListenerGps);
             Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
             //use location as we have not received the new value from listener
        }
    }
}

我们启动监听,等待一段时间(20秒在我的例子)更新。如果我们在这段时间得到更新,我们使用它。如果在此期间没有收到更新,我们使用getLastKnownLocation价值和停止监听。

We start the listener and wait for update for some time (20 seconds in my example). If we receive update during this time we use it. If we don't receive an update during this time we use getLastKnownLocation value and stop the listener.

您可以在这里http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-an/3145655#3145655

编辑(提问者):这是大多数的答案,但我的最终解决方案使用的而不是一个计时器。

EDIT (by asker): This is most of the answer, but my final solution uses a Handler instead of a Timer.

这篇关于Android的 - 可靠获取当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:47