我正在开发一个需要快速获取位置更新的应用程序,与它们的准确性无关。我需要能够每秒读取一次。我怎样才能做到这一点?
			
			1202 次
		
2 回答
            4        
        
		
除了在0中指定最小距离和最小时间值requestLocationUpdates()外,您无法控制速率。Android 会为您提供它收到的所有修复,但这是每秒 30 次修复还是每次修复 30 秒将取决于硬件、环境(例如,用户是否在室内?)等等。
于 2012-07-20T12:22:07.097   回答
    
    
            2        
        
		
您可以在 Android 位置更新和您的接收器之间构建一个层。
在您自己的层中,捕获 Android 位置更新,并将同一位置每秒 30 次传递给您的接收器,直到您获得一个新位置。
编辑
这样的东西(未经测试):
public class MyLocationManager implements LocationListener{
    private List<MyLocationListener> listeners;
    private Location lastLocation;
    private Handler handler;
    public MyLocationManager(){
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        listeners = new ArrayList<MyLocationListener>();
        handler = new Handler();
        sendLocationUpdates();
    }
    private void sendDelayedLocationUpdates(){
        handler.postDelayed(locationUpdater, 200);
    }
    public void addMyLocationListener(MyLocationListener mListener){
        listeners.add(mListener);
    }
    public void removeMyLocationListener(MyLocationListener mListener){
        listeners.remove(mListener);
    }
    @Override
    public void onLocationChanged(Location location) {
        lastLocation = location;
    }
    public interface MyLocationListener{
        public void onLocationChanged(Location location);
    }
    private Runnable locationUpdater = new Runnable(){
            @Override
            public void run(){
                for(MyLocationListener mListener : listeners){
                    mListener.onLocationChanged(lastLocation);
                }
                sendDelayedLocationUpdates();
            }
    };
}
于 2012-07-20T12:23:50.030   回答