如何持续跟踪Android手机的位置? [英] How to continuously track the location of an Android mobile phone?

查看:123
本文介绍了如何持续跟踪Android手机的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个应用程序,其中每5分钟来确定移动电话的当前位置(使用所有自由,可用的位置提供者),并将其发送到服务器。

I need to write an app, which every 5 minutes determines the current location of the mobile phone (using all free, available location providers) and sends it to a server.

如果某些位置提供没有在应用程序的启动工作,但可用之后,应用程序应该处理它的位置数据为好。

If some location provider doesn't work at the start of the application, but becomes available later, the application should process its location data as well.

为了做到这一点,我实现下面的类。在我活动之一,创建它的一个实例并调用它的 startTrackingUpdates 方法。 locationChangeHandler 确实位置数据的处理。

In order to do this, I implemented following class. In one of my activities, I create an instance of it and call its startTrackingUpdates method. locationChangeHandler does the processing of location data.

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) {
    }
}

我构建的应用程序,并在我的手机安装了它。然后,在早上我把我的电话,上班去了,然后又回了家。在家里,我检查的应用一整天的工作成果,发现只有一条记录在数据库中。

I built the application and installed it on my mobile phone. Then, in the morning I took my phone, went to work, then went back home. At home I checked the results of a day-long work of the application and found only one record in the database.

该系统提供(发送位置数据到服务器,在数据库中保存),其余正常工作,我必须在定位跟踪code一些问题( onLocationChanged 不是,虽然我改变了我的位置,几次)调用偶数。

Provided that the rest of the system (transmission of location data to the server, saving in the database) works correctly, I must have some problem in the location tracking code (onLocationChanged wasn't invoked even though I changed my location several times).

有什么毛病我的code(我应该如何改变它,以使 onLocationChanged 被调用时的手机变化超过的位置 MIN_DISTANCE_CHANGE_FOR_UPDATES 根据locatin供应商之一)?米

What's wrong with my code (how should I change it, in order for the onLocationChanged to be called whenever the location of the phone changes by more than MIN_DISTANCE_CHANGE_FOR_UPDATES meters according to one of the locatin providers) ?

更新1(2013年8月18日13:59 MSK):

我根据 MSH 的建议,改变了我的code。我用下面的code启动定时器:

I changed my code according to msh recommendations. I start the timer using following code:

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

活动我把后面的一个事件处理程序:

In the activity I put following timer event handler:

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

当电话接通时,一切都将按预期 - onActivityResult 执行一次间隔毫秒

When the phone is turned on, everything works as expected - onActivityResult is executed once in INTERVAL milliseconds.

但是,当我preSS电源键(屏幕变为禁用), onActivityResult 不是在所有的调用。当我再次preSS的电源按钮, onActivityResult 被执行多次,如间隔自从通过当时我把手机关机。如果我把手机关机了 1 *间隔毫秒,就会触发一次。如果我把手机关机了 2 *间隔毫秒,它会火的两倍等。

But when I press the power button (screen becomes disabled), onActivityResult is not invoked at all. When I press the power button again, onActivityResult is executed as many times, as INTERVAL has passed since the time when I turned the phone off. If I turned the phone off for 1 * INTERVAL milliseconds, it will fire once. If I turned the phone off for 2 * INTERVAL milliseconds, it will fire twice etc.

注:通过关闭我的意思是电源按钮的简短preSS(手机仍然生活和反应传入的短信为例)

Note: By "turning off" I mean a short press of the power button (the phone still "lives" and reacts to incoming SMS, for example).

我应该如何修改 startSavingGeoData 的code,以便该方法 onActivityResult 在每次执行间隔毫秒,即使在手机里睡觉?

How should I modify the code of startSavingGeoData in order for the method onActivityResult to be executed every INTERVAL milliseconds, even when the phone sleeps?

2更新(2013年8月18日19:29 MSK):无论 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,0,间隔,pendingIntent) ,也没有 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,间隔,间隔,pendingIntent)解决了这个问题。

Update 2 (18.08.2013 19:29 MSK): Neither alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, INTERVAL, pendingIntent), nor alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, INTERVAL, INTERVAL, pendingIntent) solved the problem.

推荐答案

您code可能是正确的,但是当您的手机正在睡觉的位置提供程序不运行。您可能需要获取wakelock(如果你不关心功耗),或使用AlarmManager并安排您的应用程序要唤醒和定期检查的位置(你还需要有积极的wakelock,而你正在等待更新,无论是通过AlamManager,或者你自己)获得的。

Your code is probably correct, but location providers are not running when your phone is sleeping. You may need to acquire a wakelock (if you don't care about power draw), or use AlarmManager and schedule your application to wake up and check location periodically (you still need to have active wakelock while you are waiting for an update, either acquired by AlamManager, or your own).

这篇关于如何持续跟踪Android手机的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆