why is onLocationChanged() triggering at unpredictable times?
I am targeting Android 1.6
The LocationManager and listener are implemented within a Service
requestLocationUpdates() is used as follows:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
//3 mins
Constants.LOCATION_UPDATE_PERIOD_MSEC,
//no distance is necessary
0,
locationListener);
The Location Manager and Listener are implemented as follows:
locationManager = (LocationManager) getSystemService(GPS_Service.this.LOCATION_SERVICE);
locationListener = new GPSlocListener(mHandler, Constants.LOCATION_UPDATE_TIME, true);
my implementation of the LocationListener:
public class GPSlocListener implements LocationListener
{
//constructor
public GPSlocListener(Handler parentMsgHandler, long timeBetweenLocationEvents, boolean accuracyOverride)
{
//store passed-in values in member variables
}
public void onLocationChanged(Location loc)
{
Log.d(TAG, "onLocationChanged() called.");
//process the data and send the location to the parentMsgHandler
}
public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
}
}
Sometimes onLocationChanged is triggered in 3 minute intervals, and sometimes at least 10 minutes can pass (possibly longer). I'll monitor the app and the little satellite icon shows up regularly. But, onLocationChanged() isn't reliably called.
some logging info:
04-07 22:08:58.945: DEBUG/GpsLocationProvider(107): Acquiring wakelock
04-07 22:08:59.186: DEBUG/libgps(107): report status : 3
04-07 22:09:05.155: DEBUG/dalvikvm(228): GC_EXPLICIT freed 13 objects / 536 bytes in 88ms
04-07 22:09:14.997: DEBUG/libgps(107): PDSM_PD_EVENT_UPDATE_FAILURE
04-07 22:09:15.005: DEBUG/libgps(107): report status : 2
04-07 22:09:15.025: DEBUG/libgps(107): report status : 1
04-07 22:09:16.035: DEBUG/libgps(107): report status : 4
04-07 22:09:16.035: DEBUG/libgps(107): report status : 3
04-07 22:09:16.035: DEBUG/libgps(107): report status : 3
04-07 22:09:31.065: DEBUG/libgps(107): report status : 2
04-07 22:09:31.125: DEBUG/libgps(107): report status : 1
04-07 22:09:32.145: DEBUG/libgps(107): report status : 4
04-07 22:09:32.145: DEBUG/libgps(107): report status : 3
04-07 22:09:32.145: DEBUG/libgps(107): report status : 3
04-07 22:09:47.165: DEBUG/libgps(107): report status : 2
04-07 22:09:47.185: DEBUG/libgps(107): report status : 1
04-07 22:09:48.195: DEBUG/libgps(107): report status : 4
04-07 22:09:48.381: DEBUG/libgps(107): report status : 3
04-07 22:09:48.381: DEBUG/libgps(107): report status : 3
04-07 22:09:48.615: DEBUG/LocationManagerService(107): CdmaCellLocation Available
04-07 22:09:48.625: VERBOSE/AlarmManager(107): Adding Alarm{46208250 type 2 com.google.android.apps.maps} Dec 13 12:46:54 pm
04-07 22:09:51.855: DEBUG/LocationManagerService(107): CdmaCellLocation Available
04-07 22:09:51.865: VERBOSE/AlarmManager(107): Adding Alarm{461e4e88 type 2 com.google.android.apps.maps} Dec 13 12:46:54 pm
04-07 22:09:56.895开发者_Python百科: DEBUG/dalvikvm(206): GC_EXPLICIT freed 172 objects / 6912 bytes in 58ms
04-07 22:09:58.915: VERBOSE/AlarmManager(107): Alarm triggering: Alarm{464f6cc8 type 2 android}
04-07 22:09:58.925: DEBUG/GpsLocationProvider(107): ALARM_TIMEOUT
04-07 22:09:58.925: DEBUG/GpsLocationProvider(107): stopNavigating
04-07 22:09:58.925: DEBUG/libgps(107): qct_gps_stop
04-07 22:09:58.925: DEBUG/libgps(107): report status : 2
04-07 22:09:58.925: VERBOSE/AlarmManager(107): Adding Alarm{46540430 type 2 android} Jan 01 05:22:56 am
04-07 22:10:00.965: DEBUG/libgps(107): report status : 4
04-07 22:10:00.965: DEBUG/GpsLocationProvider(107): Releasing wakelock
and the permissions:
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and to give you an idea, this is a typical result:
04-07 21:42:46.985: DEBUG/GPSlocListener(2582): onLocationChanged() called.
04-07 21:42:58.515: DEBUG/GPSlocListener(2582): onLocationChanged() called.
04-07 21:45:14.825: DEBUG/GPSlocListener(2582): onLocationChanged() called.
04-07 21:48:14.865: DEBUG/GPSlocListener(2582): onLocationChanged() called.
04-07 21:48:26.205: DEBUG/GPSlocListener(2582): onLocationChanged() called.
04-07 21:48:57.815: DEBUG/GPSlocListener(2582): onLocationChanged() called.
04-07 21:54:30.995: DEBUG/GPSlocListener(2582): onLocationChanged() called.
04-07 21:54:42.375: DEBUG/GPSlocListener(2582): onLocationChanged() called.
04-07 21:54:54.395: DEBUG/GPSlocListener(2582): onLocationChanged() called.
The onLocationChanged will be triggered when the location of the device changes. if you are sitting in one place and the location has not changed, then the onLocationChanged might not be triggered. So of that is the case then your program is working fine and LocationManager is also behaving as expected.
Try changing the Log.d to show a toast as your location is changed and try walking or moving your position. You will see the updated location toasts as your location changes. Try decreasing the minimum interval for your test cases ;). Also keep in mind that GPS locations will not be accurate if you are fetching the location when you are in a building.
Achie.
精彩评论