我开发了一个应用程序,每30秒后使用当前位置。该应用程序已完成。它在ICS(Android 4),JellyBean(Android 4.1)设备上运行完美,但是当我在Android 2.3或Android 2.2上运行应用程序时,应用程序GPS每2/3秒更新一次并不断更新。虽然我的代码完成了30秒,但它在Android 4,4.1中完美运行(30秒后)但不适用于Android 2.3或2.2。
我正在分享代码......
public class GPSManager
{
//private static final int gpsMinTime = 60000;
//private static final int gpsMinDistance = 0;
private long gpsMinTime = 0;
private float gpsMinDistance = 0;
private static LocationManager locationManager = null;
private static LocationListener locationListener = null;
private static GPSCallback gpsCallback = null;
public GPSManager(long gpsMinTime,float gpsMinDistance)
{
this.gpsMinTime = gpsMinTime;
this.gpsMinDistance = gpsMinDistance;
GPSManager.locationListener = new LocationListener()
{
@Override
public void onLocationChanged(final Location location)
{
if (GPSManager.gpsCallback != null)
{
GPSManager.gpsCallback.onGPSUpdate(location);
}
}
@Override
public void onProviderDisabled(final String provider)
{
}
@Override
public void onProviderEnabled(final String provider)
{
}
@Override
public void onStatusChanged(final String provider, final int status, final Bundle extras)
{
}
};
}
public GPSCallback getGPSCallback()
{
return GPSManager.gpsCallback;
}
public void setGPSCallback(final GPSCallback gpsCallback)
{
GPSManager.gpsCallback = gpsCallback;
}
public void startListening(final Context context)
{
if (GPSManager.locationManager == null)
{
GPSManager.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_MEDIUM); // accuracy_midium
criteria.setSpeedRequired(true);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(true);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
final String bestProvider = GPSManager.locationManager.getBestProvider(criteria, true);
if (bestProvider != null && bestProvider.length() > 0)
{
GPSManager.locationManager.requestLocationUpdates(bestProvider, this.gpsMinTime,
this.gpsMinDistance, GPSManager.locationListener);
}
else
{
final List<String> providers = GPSManager.locationManager.getProviders(true);
for (final String provider : providers)
{
GPSManager.locationManager.requestLocationUpdates(provider, this.gpsMinTime,
this.gpsMinDistance, GPSManager.locationListener);
}
}
}
public void stopListening()
{
try
{
if (GPSManager.locationManager != null && GPSManager.locationListener != null)
{
GPSManager.locationManager.removeUpdates(GPSManager.locationListener);
}
GPSManager.locationManager = null;
}
catch (final Exception ex)
{
}
}
}
我的位置更新代码在这里:
private static final long GPS_MINTIME = 30000;
private static final float GPS_MINDISTANCE = 100;
gpsManager = new GPSManager(GPS_MINTIME,GPS_MINDISTANCE);
gpsManager.startListening(getApplicationContext());
gpsManager.setGPSCallback(this);
任何想法为什么会这样。感谢。
答案 0 :(得分:0)
将GPS_MINTIME
更改为6000
以上它将完美无缺。
答案 1 :(得分:0)
请在SO上查看此答案 https://stackoverflow.com/a/4245527/1438915
in api&lt; 4.0分钟的时间不是强制性的 考虑到它有时可能不准确。 的作用类似于提示。
<强>更新强>
来自文档INFO的我认为您应该阅读此内容。
答案 2 :(得分:0)
继我的评论之后,这是一个已知的(但没有广泛报道的错误/功能)。见 Google Groups Post 我确实添加了对此提案的支持,但该线程现已关闭,因为它已在4.1
中修复