Android - 可靠地获取当前位置

时间:2023-03-22
本文介绍了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.

我可能错了没有调用听众,这可能比我想象的要长一点......很难说.我仍然希望得到一个明确的答案.

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.

推荐答案

代码可能是这样的:

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.

您可以在此处查看我的完整代码 在 Android 上获取用户当前位置的最简单、最可靠的方法是什么?

You can see my complete code here What is the simplest and most robust way to get the user's current location on Android?

编辑(由提问者):这是大部分答案,但我的最终解决方案使用 Handler 而不是 Timer.

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

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

上一篇:Android:使用 LocationManager.requestLocationUpdates() 时如何从 Int 下一篇:从 iOS 获取反向地理编码当前位置的最简单方法

相关文章