我瞄準了Android 1.6爲什麼onLocationChanged()在不可預知的時間觸發?
的的LocationManager和聽衆是服務
requestLocationUpdates()作爲中實現如下:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
//3 mins
Constants.LOCATION_UPDATE_PERIOD_MSEC,
//no distance is necessary
0,
locationListener);
位置管理器和監聽如下實施:
locationManager = (LocationManager) getSystemService(GPS_Service.this.LOCATION_SERVICE);
locationListener = new GPSlocListener(mHandler, Constants.LOCATION_UPDATE_TIME, true);
我實施的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
}
}
有時onLocationChanged以3分鐘間隔觸發,有時至少10分鐘可以通過(可能更長)。 我會監視應用程序,並定期顯示小衛星圖標。但是,不能可靠地調用onLocationChanged()。
一些記錄信息:
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: 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
和權限:
<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" />
,並給你一個想法,這是一個典型的結果:
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.
哦,這對我來說是個壞消息:-(我希望requestLocationUpdates()不會考慮距離,因爲我傳入一個0作爲距離參數。 – 2011-04-08 06:43:23
我發現這篇文章,它解釋了一些東西太:http://www.android10.org/index.php/articleslocationmaps/294-how-locationlistener-works-in-android – 2011-04-09 01:29:43
〜「如果你坐在一個地方,位置沒有改變,那麼onLocationChanged將不會被觸發「,這是不正確的,我已經測試了它坐在一個地方,並且該設備可能仍然認爲該位置改變了! – 2013-11-12 17:07:16