为什么onLocationChanged()在不可预测的时间触发?

时间:2011-04-08 05:04:45

标签: android

我的目标是Android 1.6

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. 

1 个答案:

答案 0 :(得分:0)

当设备的位置发生变化时,将触发onLocationChanged。如果您坐在一个地方并且位置没有改变,那么可能不会触发onLocationChanged。所以就是这样,那么你的程序运行正常,而且LocationManager的行为也符合预期。

尝试更改Log.d以在您的位置发生变化时显示祝酒词并尝试步行或移动您的位置。随着您的位置变化,您将看到更新的位置祝酒词。尝试减少测试用例的最小间隔;)。另请注意,如果您在建筑物内时取出该位置,GPS位置将不准确。

的政绩。