我的应用需要使用新的当前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更新?
谢谢你
答案 0 :(得分:2)
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或网络提供商来获取位置。 --->它工作和新代码:
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, locationListenerGps);
原因是,我不使用Network_Provider。因为,当2个GPS和网络提供商,系统将使用NEtwork_provider。但是在logcat中,我看到智能手机没有使用Network_provider更新“Listenter”循环3-6s。 En revanche,GPS_PROVIDER,智能手机更新3-6s。
- 第一次打开GPS;我需要30-50秒才能拥有第一个Listener。但没关系
答案 1 :(得分:2)
我写了一个小应用程序,它将返回位置,平均速度和当前速度。当我在手机上运行它(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
}
}
答案 2 :(得分:0)
requestLocationUpdates
的minTime参数应为3000到8000
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener, Looper looper)
minTime the minimum
通知的时间间隔,以毫秒为单位。此字段仅用作节省电量的提示,位置更新之间的实际时间可能大于或小于此值。
minDistance
通知的最小距离间隔,以米为单位
答案 3 :(得分:0)
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)
自:API Level 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)方法