安卓:requestLocationUpdates最多每45秒更新的位置 [英] Android: requestLocationUpdates updates location at most every 45 seconds

查看:299
本文介绍了安卓:requestLocationUpdates最多每45秒更新的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个Android应用程序,其主要功能是跟踪用户的位置,当用户得到附近的一些问题作出警报。因此,我需要定期更新该用户的位置,并且这些间隔应该变小作为用户越接近目标。所以,当用户是内,就是说,在目标的1公里,我想要的位置可以每20秒更新等等,直到用户到达

I am writing an Android app whose main function is tracking the user's location and making an alert when the user gets near some point. Therefore I need to update the user's location at regular intervals, and these intervals should get smaller as the user comes closer to the target. So when the user is within, say, 1 km of the target, I want the location to be updated every 20 seconds and so on, until the user arrives.

当我测试它(提供商= LocationManager.NETWORK_PROVIDER ),调用 requestLocationUpdates(供应商,minTime,minDistance依旧,LocationListener的)任何 minTime&LT; 45000 有同样的效果 minTime = 45000 ,也就是我得到的的完全的45秒的间隔更新。< BR> 我知道最小时间参数只有一个暗示,但它不取为暗示通过我的应用程序。我得到更新与指定直到间隔传递低于45秒的时间间隔。它好像45秒的位置更新之间的最短时间是很难coded进入Android的,但是这将是一种奇怪的。另外,我从来没有听说过这个问题之前,我一直没能找到它在这里讨论的StackOverflow。

When I test it (provider = LocationManager.NETWORK_PROVIDER), a call to requestLocationUpdates(provider, minTime, minDistance, locationListener) with any minTime < 45000 has the same effect as minTime = 45000, i.e. I get updates with an interval of exactly 45 seconds.
I know the minimum time parameter is only a "hint", but it is not taken as a hint by my app. I get updates with the interval specified until that interval passes below 45 seconds. It seems as though a minimum time of 45 seconds between location updates is hardcoded into Android, but that would be kind of odd. Plus I have never heard of this problem before, and I have not been able to find it addressed here on Stackoverflow.

由于我没有能够得到频繁的更新,我的解决方法(现在)是手动调用 requestLocationUpdates 每当一个新的位置是必要的,然后只使用第一可用的位置。要做到这一点以小间隔我用 handler.postDelayed(myRunnable,updateInterval)拖延通话, myRunnable 然后它会调用照顾 requestLocationUpdates 。但是,这种方法只适用的时间约为50(显然是随机的)百分比

Because I am not able to get frequent updates, my workaround (for now) is to manually call requestLocationUpdates whenever a new location is needed, and then just use the first available location. To do this at small intervals I use handler.postDelayed(myRunnable, updateInterval) to delay the calls, and myRunnable then takes care of calling requestLocationUpdates. However, this method only works about 50 (apparently random) percent of the time.

有谁知道这个问题的,并且是有办法解决这个问题?或者是我唯一的选择来设置 minTime = 0 ,只希望最好?

Does anybody know of the problem, and is there a way to fix it? Or is my only option to set minTime = 0 and just hope for the best?

下面是源$ C ​​$下myRunnable,其的run()方法,我经常手动调用与 handler.postDelayed(myRunnable,updateInterval )

Here is the source code for myRunnable, whose run() method I manually call regularly with handler.postDelayed(myRunnable, updateInterval):

public class MyRunnable implements Runnable {
    private LocationManager manager;
    private LocationListener listener;

    @Override
    public void run() {
        // This is called everytime a new update is requested
        // so that only one request is running at a time.
        removeUpdates();

        manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        listener = new LocationListener() {
            @Override
            public void onLocationChanged(Location loc) {
                location = loc;
                latitude = loc.getLatitude();
                longitude = loc.getLongitude();
                accuracy = Math.round(loc.getAccuracy());

                handler.sendMessage(Message.obtain(handler, KEY_MESSAGE_LOCATION_CHANGED));

                checkForArrival();
            }

            // Other overrides are empty.
        };

        if(!arrived)
            manager.requestLocationUpdates(provider, updateInterval, 0, listener);
    }

    /**
     * Removes location updates from the LocationListener.
     */
    public void removeUpdates() {
        if(!(manager == null || listener == null))
            manager.removeUpdates(listener);
    }

    // Another method for "cleaning up" when the user has arrived.
}


这里是我的处理


And here is my handler:

handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
            case KEY_MESSAGE_LOCATION_CHANGED:
                if(myRunnable != null) {
                    myRunnable.removeUpdates();
                    handler.postDelayed(myRunnable, updateInterval);
                }
                break;
            }
        }
    };



整个位置更新的东西运行在一个服务。

The whole location updating thing runs in a service.

我已阅读文档几次,Google'd问题,并尝试各种其他的解决方法。什么能做到的。

I have read the doc several times, Google'd the problem, and tried various other workarounds. Nothing quite does it.

我已经登录了该死的出了这种事,而唯一令人兴奋的事情看到的是一个大胖子忽略我频繁的位置请求。所有正确的方法被调用。

I have logged the damn out of this thing, and the only exciting thing to see is a big fat "ignore" to my frequent location requests. All the right methods are called.

任何帮助将是的非常的AP preciated!

Any help will be very much appreciated!

推荐答案

您可以设置minTime为任意值。但是,你只会得到一个更新一次新的位置是可用的。该网络只更新每隔45秒左右,我自己的每一个电话。这似乎是网络提供程序的限制。如果你想更频繁的更新使用GPS提供商。根据GPS硬件,你应该避开4Hz的最大更新率。

You can set the minTime to any value. However, you will only get an update once a new location is available. The network only updates every 45 sec or so on every phone I own. This seems to be a limitation of the Network Provider. If you want more frequent updates use the GPS provider. Depending on the GPS hardware you should get a maximum update rate around 4Hz.

这篇关于安卓:requestLocationUpdates最多每45秒更新的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆