Android:如何保持 GPS 活动,直到提供更准确的位置?

发布于 2024-08-16 19:18:13 字数 338 浏览 3 评论 0原文

我正在使用位置管理器的 requestLocationUpdates() 方法定期接收广播接收器的意图。系统正确地将意图发射到我的接收器,并且我已经能够正确使用它。唯一的问题是 GPS 位置提供程序在初始位置获取后仅保持活动状态几秒钟,而我需要它保持更长的时间,以便位置估计更加准确。

我的问题是如何使 GPS 位置提供程序对来自 LocationManager 请求位置更新。有谁知道该怎么做?

I am using the location manager's requestLocationUpdates() method to receive an intent to my broadcast receiver periodically. The system is correctly firing the intent to my receiver, and I have been able to use it correctly. The only problem is that the GPS location provider only stays active for a few seconds after the initial location acquisition, and I need it to stay on a little longer so that the location estimates are more accurate.

My question is how to make the GPS location provider stay active for each periodic request that comes from the LocationManager requestLocationUpdates. Does anyone know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦与时光遇 2024-08-23 19:18:13

尝试这样的事情。我认为这是正确的做法

private void createGpsListner()
{
    gpsListener = new LocationListener(){
        public void onLocationChanged(Location location)
        {
           curLocation = location;

           // check if locations has accuracy data
           if(curLocation.hasAccuracy())
           {
               // Accuracy is in rage of 20 meters, stop listening we have a fix
               if(curLocation.getAccuracy() < 20)
               {
                   stopGpsListner();
               }
           }
        }
        public void onProviderDisabled(String provider){}
        public void onProviderEnabled(String provider){}
        public void onStatusChanged(String provider, int status, Bundle extras){}
    };
}

private void startGpsListener()
{

    if(myLocationManager != null)
        // hit location update in intervals of 5sec and after 10meters offset
        myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, gpsListener);   
}

private void stopGpsListner()
{
    if(myLocationManager != null)
        myLocationManager.removeUpdates(gpsListener);
}

Try something like this. I think it is the right approach

private void createGpsListner()
{
    gpsListener = new LocationListener(){
        public void onLocationChanged(Location location)
        {
           curLocation = location;

           // check if locations has accuracy data
           if(curLocation.hasAccuracy())
           {
               // Accuracy is in rage of 20 meters, stop listening we have a fix
               if(curLocation.getAccuracy() < 20)
               {
                   stopGpsListner();
               }
           }
        }
        public void onProviderDisabled(String provider){}
        public void onProviderEnabled(String provider){}
        public void onStatusChanged(String provider, int status, Bundle extras){}
    };
}

private void startGpsListener()
{

    if(myLocationManager != null)
        // hit location update in intervals of 5sec and after 10meters offset
        myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, gpsListener);   
}

private void stopGpsListner()
{
    if(myLocationManager != null)
        myLocationManager.removeUpdates(gpsListener);
}
痴情换悲伤 2024-08-23 19:18:13

如果您保持 LocationListener 处于活动状态,那么如果修复精度变窄,它应该会继续接收 onLocationChanged() 的更新。事实上,location.getAccuracy() 会告诉你当前的精度,

也许将 minTime 和 minDistance 都设置为 0 以便以更高的频率接收更新?将使用更多电池,但更精确。

if you keep your LocationListener active, it should continue to receive updates to onLocationChanged() if the fix accuracy narrows. and indeed location.getAccuracy() will tell you the current accuracy

maybe set minTime and minDistance both to 0 to receive updates with greater frequency? will use more battery, but is more preise.

死开点丶别碍眼 2024-08-23 19:18:13

有一个关于超时获取 GPS 位置的示例。

http://sikazi.blogspot.com/2010/09/ android-gps-timeout.html#more

There is a example about get GPS location with timeout.

http://sikazi.blogspot.com/2010/09/android-gps-timeout.html#more

云仙小弟 2024-08-23 19:18:13

要定期获取 GPS 位置,请从 locationListeneronLocationChanged 方法获取位置,并在 onResume 方法中指定以毫秒为单位的时间 用于获取定期更新

onResume
location_manager.requestLocationUpdates(provider, 1000, 1, MainActivity.this);

To get GPS location periodically, get the location from onLocationChanged method of locationListener and in onResume method specify the timing in milliseconds for getting periodic updates

onResume
location_manager.requestLocationUpdates(provider, 1000, 1, MainActivity.this);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文