Android:如何让GPS保持活动状态,直到提供更精确的位置? [英] Android: How to keep GPS active until more accurate location is provided?

查看:88
本文介绍了Android:如何让GPS保持活动状态,直到提供更精确的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用位置管理器的requestLocationUpdates()方法来定期接收我的广播接收器的意图。系统正确地向我的接收器发射意图,并且我能够正确使用它。唯一的问题是,GPS位置提供商在初始位置获取后仅保持激活状态几秒钟,我需要稍微延长一点,以便位置估算更准确。

我的问题是如何使GPS位置提供程序保持活动状态,以便来自 LocationManager requestLocationUpdates。有没有人知道如何做到这一点? 试试类似这样的东西。我认为这是正确的做法

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

//检查位置是否有准确性数据
if(curLocation.hasAccuracy())
{
//准确度在20米的范围内,停止听我们如果(curLocation.getAccuracy()<20)
{
stopGpsListner();


$ b public void onProviderDisabled(String provider){} $ b $ public void onProviderEnabled(String provider){} $ b $ public void onStatusChanged(String provider,int status,Bundle extras){}
};
}

private void startGpsListener()
{

if(myLocationManager!= null)
//按时间间隔点击位置更新5sec和10米后偏移
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,10,gpsListener);
}

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


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?

解决方案

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);
}

这篇关于Android:如何让GPS保持活动状态,直到提供更精确的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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