我开发了一个应用程序,每 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);
任何想法为什么会发生这种情况。谢谢。