如何连续跟踪Android手机的位置?

DP_*_*DP_ 10 android geolocation

我需要编写一个应用程序,每隔5分钟确定一次移动电话的当前位置(使用所有免费的可用位置提供商)并将其发送到服务器.

如果某个位置提供程序在应用程序启动时不起作用,但稍后可用,则应用程序也应处理其位置数据.

为了做到这一点,我实施了以下课程.在我的一个活动中,我创建了一个实例并调用它的startTrackingUpdates方法.locationChangeHandler处理位置数据.

public class LocationTracker implements ILocationTracker, LocationListener {
    public static final long MIN_TIME_BETWEEN_UPDATES = 1000 * 60 * 5; // 5 minutes
    public static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
    private ILocationManager locationManager;
    private ILocationChangeHandler locationChangeHandler;

    public LocationTracker(final ILocationManager aLocationManager,
            final ILocationChangeHandler aLocationChangeHandler) {
        this.locationManager = aLocationManager;
        this.locationChangeHandler = aLocationChangeHandler;
    }

    public void startTrackingUpdates() {
        final List<String> providers = locationManager.getAllProviders();

        for (final String curProviderName : providers)
        {
            final ILocationProvider provider = locationManager.getLocationProvider(curProviderName);

            if (!provider.hasMonetaryCost())
            {
                subscribeToLocationChanges(provider);
            }
        }
    }

    private void subscribeToLocationChanges(final ILocationProvider aProvider) {
        locationManager.requestLocationUpdates(aProvider.getName(), MIN_TIME_BETWEEN_UPDATES, 
                (float)MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    }

    public void onLocationChanged(final Location aLocation) {
        locationChangeHandler.processLocationChange(new LocationWrapper(aLocation));
    }

    public void onProviderDisabled(final String aProvider) {
    }

    public void onProviderEnabled(final String aProvider) {
    }

    public void onStatusChanged(final String aProvider, final int aStatus, 
            final Bundle aExtras) {
    }
}
Run Code Online (Sandbox Code Playgroud)

我构建了应用程序并将其安装在我的手机上.然后,早上我拿起手机,上班,然后回家.在家里,我检查了应用程序一天工作的结果,发现数据库中只有一条记录.

如果系统的其余部分(将位置数据传输到服务器,保存在数据库中)正常工作,我必须在位置跟踪代码onLocationChanged中遇到一些问题(即使我多次更改了我的位置,也没有调用).

我的代码有什么问题(我应该如何更改它,以便onLocationChanged每当手机的位置MIN_DISTANCE_CHANGE_FOR_UPDATES根据其中一个定位器提供者的位置变化超过米时调用它?)

更新1(18.08.2013 13:59 MSK):

我根据msh推荐改变了我的代码.我使用以下代码启动计时器:

public void startSavingGeoData() {
    final IAlarmManager alarmManager = activity.getAlarmManager();
    final IPendingIntent pendingIntent = activity.createPendingResult(
            ALARM_ID, intentFactory.createEmptyIntent(), 0);
    alarmManager.setRepeating(INTERVAL, pendingIntent);
}
Run Code Online (Sandbox Code Playgroud)

activity我把下面的定时器事件处理程序:

@Override
protected void onActivityResult(final int aRequestCode, final int aResultCode, 
        final Intent aData) {
    geoDataSender.processOnActivityResult(aRequestCode, aResultCode, 
            new IntentWrapper(aData));
}
Run Code Online (Sandbox Code Playgroud)

当手机开启时,一切都按预期工作 - onActivityResultINTERVAL毫秒为单位执行一次.

但是,当我按下电源按钮(屏幕变为禁用)时,onActivityResult根本不会调用.当我再次按下电源按钮时,onActivityResult执行多次,就像INTERVAL我关闭手机时一样.如果我关闭手机1 * INTERVAL几毫秒,它将触发一次.如果我把手机关掉2 * INTERVAL几毫秒,它会发射两次等.

注意:通过"关闭",我的意思是短按电源按钮(例如,手机仍然"活着"并对传入的短信作出反应).

我应该如何修改代码,startSavingGeoData以便onActivityResultINTERVAL毫秒执行一次方法,即使手机处于休眠状态?

更新2(18.08.2013 19:29 MSK):既没有alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, INTERVAL, pendingIntent),也没有alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, INTERVAL, INTERVAL, pendingIntent)解决问题.

msh*_*msh 8

您的代码可能是正确的,但是当您的手机正在睡眠时,位置提供商没有运行.您可能需要获取唤醒锁(如果您不关心电源抽取),或使用AlarmManager并安排您的应用程序定期唤醒并检查位置(您在等待更新时仍需要有活动的唤醒锁,或者由AlamManager或您自己获得.