强制更新多次:Gps位置持续3-6秒。

我的应用需要使用新的当前GPS参数(每3到8秒更新一次):纬度和经度。我同时使用:GPS提供程序和网络提供程序。 我知道用来更新GPS参数
if(gps_enabled)
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);

if(network_enabled)
     lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
问题:实际上,gps在每个环境40-50秒后更新 3-8秒后如何获取GPS更新? 谢谢     
已邀请:
        
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
                try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
// lm是locationManager 事实上。我不使用以下条件:network_enabled或Network-provider获取位置。 --->它和新代码一起工作:
 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, locationListenerGps);
原因,我不使用Network_Provider。因为,当2 GPS和Network Provider时,系统将使用NEtwork_provider。但是在日志猫中,我看到智能手机无法使用Network_provider更新“ Listenter”循环3-6s。 启用时,使用GPS_PROVIDER可以3-6秒钟更新智能手机。 -第一次打开GPS;我需要30-50秒才能拥有第一个侦听器。不过还可以     
        我已经编写了一个小应用程序,可以返回位置,平均速度和当前速度。当我在手机上运行它(HTC Legend)时,它每秒更新1-2次。如果您愿意,欢迎您使用它。您只需要创建一个具有6个textviews的main.xml文件,然后将此行添加到您的AndroidManifest.xmll文件中即可:
package Hartford.gps;

import java.math.BigDecimal;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class GPSMain extends Activity implements LocationListener {

LocationManager locationManager;
LocationListener locationListener;

//text views to display latitude and longitude
TextView latituteField;
TextView longitudeField;
TextView currentSpeedField;
TextView kmphSpeedField;
TextView avgSpeedField;
TextView avgKmphField;

//objects to store positional information
protected double lat;
protected double lon;

//objects to store values for current and average speed
protected double currentSpeed;
protected double kmphSpeed;
protected double avgSpeed;
protected double avgKmph;
protected double totalSpeed;
protected double totalKmph;

//counter that is incremented every time a new position is received, used to calculate average speed
int counter = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    run();
}

@Override
public void onResume() {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this);
    super.onResume();
}

@Override
public void onPause() {
    locationManager.removeUpdates(this);
    super.onPause();
}

private void run(){

    final Criteria criteria = new Criteria();

    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setSpeedRequired(true);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    //Acquire a reference to the system Location Manager

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates
    locationListener = new LocationListener() {

        public void onLocationChanged(Location newLocation) {

            counter++;

            //current speed fo the gps device
            currentSpeed = round(newLocation.getSpeed(),3,BigDecimal.ROUND_HALF_UP);
            kmphSpeed = round((currentSpeed*3.6),3,BigDecimal.ROUND_HALF_UP);

            //all speeds added together
            totalSpeed = totalSpeed + currentSpeed;
            totalKmph = totalKmph + kmphSpeed;

            //calculates average speed
            avgSpeed = round(totalSpeed/counter,3,BigDecimal.ROUND_HALF_UP);
            avgKmph = round(totalKmph/counter,3,BigDecimal.ROUND_HALF_UP);

            //gets position
            lat = round(((double) (newLocation.getLatitude())),3,BigDecimal.ROUND_HALF_UP);
            lon = round(((double) (newLocation.getLongitude())),3,BigDecimal.ROUND_HALF_UP);

            latituteField = (TextView) findViewById(R.id.lat);
            longitudeField = (TextView) findViewById(R.id.lon);             
            currentSpeedField = (TextView) findViewById(R.id.speed);
            kmphSpeedField = (TextView) findViewById(R.id.kmph);
            avgSpeedField = (TextView) findViewById(R.id.avgspeed);
            avgKmphField = (TextView) findViewById(R.id.avgkmph);

            latituteField.setText(\"Current Latitude:        \"+String.valueOf(lat));
            longitudeField.setText(\"Current Longitude:      \"+String.valueOf(lon));
            currentSpeedField.setText(\"Current Speed (m/s):     \"+String.valueOf(currentSpeed));
            kmphSpeedField.setText(\"Cuttent Speed (kmph):       \"+String.valueOf(kmphSpeed));
            avgSpeedField.setText(\"Average Speed (m/s):     \"+String.valueOf(avgSpeed));
            avgKmphField.setText(\"Average Speed (kmph):     \"+String.valueOf(avgKmph));

        }

        //not entirely sure what these do yet
        public void onStatusChanged(String provider, int status, Bundle extras) {}
        public void onProviderEnabled(String provider) {}
        public void onProviderDisabled(String provider) {}

    };

    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, locationListener);
}

//Method to round the doubles to a max of 3 decimal places
public static double round(double unrounded, int precision, int roundingMode)
{
    BigDecimal bd = new BigDecimal(unrounded);
    BigDecimal rounded = bd.setScale(precision, roundingMode);
    return rounded.doubleValue();
}


@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

}
    
        Time4ѭ的minTime参数应为3000到8000
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener, Looper looper)
minTime    the minimum
通知的时间间隔(以毫秒为单位)。该字段仅用作节省电量的提示,位置更新之间的实际时间可能大于或小于此值。
minDistance
通知的最小距离间隔,以米为单位     
        看看requestLocationUpdates(...)
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)
  由于:API级别1      注册当前活动以由指定提供者定期通知。定期,将使用当前位置或状态更新来调用提供的LocationListener。      可能需要一段时间才能收到最新位置。如果需要立即定位,则应用程序可以使用getLastKnownLocation(String)方法。      如果用户禁用了提供程序,更新将停止,并且将调用onProviderDisabled(String)方法。再次启用提供程序后,将调用onProviderEnabled(String)方法,并且位置更新将再次开始。      可以使用minTime和minDistance参数控制通知的频率。如果minTime大于0,则LocationManager可能会在两次位置更新之间休息minTime毫秒,以节省电量。如果minDistance大于0,则仅当设备移动minDistance米时,才会广播位置。要尽可能频繁地获取通知,请将两个参数都设置为0。      后台服务在设置足够高的minTime时应格外小心,以使设备始终保持GPS或无线电的开启状态,从而不会消耗太多电能。特别是,建议不要使用60000ms以下的值。      调用线程必须是Looper线程,例如调用Activity的主线程。      参量      提供者要注册的提供者的名称      minTime通知的最小时间间隔(以毫秒为单位)。该字段仅用作节省电量的提示,位置更新之间的实际时间可能大于或小于此值。      minDistance通知的最小距离间隔,以米为单位      侦听器,一个{#link LocationListener},其onLocationChanged(Location)方法将在每次位置更新时被调用     

要回复问题请先登录注册