代码之家  ›  专栏  ›  技术社区  ›  jul

在Android中获取更新的位置

  •  0
  • jul  · 技术社区  · 15 年前

    每次单击按钮时,我都使用下面显示的代码获取位置的更新值。当我的活动恢复时,我会每秒更新一次,因此当我调用GetLastKnownLocation时,我希望在最后一秒更新一个位置。

    这是正确的方法吗?

    我希望每次执行“geo fix”命令时都触发onlocationchanged事件(或者自我请求每隔1秒更新以来,最多1秒后触发),但它只是第一次触发。为什么?

    欢迎提供任何帮助/建议!

    谢谢

    package org.digitalfarm.atable;
    
    ...
    
    public class Atable extends Activity {
        private Button mSearchButton;
        private TextView mytext;
        private LocationManager locationManager;    
    
    
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);   
    
            setContentView(R.layout.main);
    
            mSearchButton = (Button)this.findViewById(R.id.button);
    
            mytext = (TextView) findViewById(R.id.dude);
    
            locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            final Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);               
    
    
            mSearchButton.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
    
                      String provider = locationManager.getBestProvider(criteria, true);
    
                      Location location = locationManager.getLastKnownLocation(provider);             
    
              }
            });
        }
    
         //Start a location listener
        LocationListener onLocationChange=new LocationListener() {
            public void onLocationChanged(Location loc) {
                //sets and displays the lat/long when a location is provided
                String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();   
                mytext.setText(latlong);
            }
    
            public void onProviderDisabled(String provider) {
            // required for interface, not used
            }
    
            public void onProviderEnabled(String provider) {
            // required for interface, not used
            }
    
            public void onStatusChanged(String provider, int status,
            Bundle extras) {
            // required for interface, not used
            }
        };
    
        //pauses listener while app is inactive
        @Override
        public void onPause() {
            super.onPause();
            locationManager.removeUpdates(onLocationChange);
        }
    
        //reactivates listener when app is resumed
        @Override
        public void onResume() {
            super.onResume();
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,100.0f,onLocationChange);
        }
    }
    
    1 回复  |  直到 15 年前
        1
  •  0
  •   Shade    15 年前

    在RequestLocationUpdates中指定的时间是最短的时间间隔,最多只能进行一次更新。因此,您每秒钟最多注册一次更新,但实际更新可能会花费更多的时间,而且,这一点没有任何保证。

    从文档中:

    明泰 -通知的最短时间间隔(毫秒)。此字段仅用作省电提示,以及 位置更新之间的实际时间可能大于或小于 大于此值。

    尝试将时间间隔设置为0,如果没有其他问题(尽管对我来说一切似乎都很好),您应该“尽可能频繁地”开始获取更新。

    推荐文章