3

我目前正在开发一个应用程序,该应用程序必须每五分钟检查一次用户的位置并将坐标发送到服务器。我决定使用 Google Play 服务中的 FusedLocation API 而不是普通的旧 LocationManager API,主要是因为我注意到LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY优先级,它声称提供 100 米精度级别和合理的电池使用,这正是我需要。

就我而言,我有一个 Activity,其继承结构是:

public class MainActivity extends AppCompatActivity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener

并实现了相关的回调(onConnected、onConnectionFailed、onConnectionSuspended、onLocationChanged)。根据官方文档的建议,我还使用此方法获得了 GoogleApiClient 的实例:

protected synchronized GoogleApiClient buildGoogleApiClient() {
        return new GoogleApiClient.Builder(this).addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();

在 onConnected 中,我使用

LocationServices.FusedLocationApi.requestLocationUpdates(mApiClient,
                mLocationRequest, this);

...并捕获 onLocationChanged() 中的更改。

但是,我很快发现位置更新似乎在一段时间后停止了。也许是因为这个方法与 Activity 生命周期相关,我不确定。无论如何,我试图通过创建一个扩展 IntentService 的内部类并通过 AlarmManager 启动它来解决这个问题。所以在 onConnected 中,我最终这样做了:

AlarmManager alarmMan = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);

        Intent updateIntent = new Intent(this, LocUpService.class);

        PendingIntent pIntent = PendingIntent.getService(this, 0, updateIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        alarmMan.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0,
                1000 * 60 * 5, pIntent);

LocUpService 类如下所示:

public static class LocUpService extends IntentService {

        public LocUpService() {
            super("LocUpService");

        }

        @Override
        protected void onHandleIntent(Intent intent) {
            Coords coords = LocationUpdater.getLastKnownLocation(mApiClient);


        }

    }

LocationUpdater 是另一个类,它包含静态方法 getLastKnownLocation,它是这样的:

public static Coords getLastKnownLocation(GoogleApiClient apiClient) {

        Coords coords = new Coords();
        Location location = LocationServices.FusedLocationApi
                .getLastLocation(apiClient);

        if (location != null) {

            coords.setLatitude(location.getLatitude());
            coords.setLongitude(location.getLongitude());

            Log.e("lat ", location.getLatitude() + " degrees");
            Log.e("lon ", location.getLongitude() + " degrees");

        }
        return coords;
    }

但是惊喜!!当我清楚地将引用传递给静态方法时,我得到“IllegalArgumentException:需要 GoogleApiClient 参数”,我再次猜测这肯定与 GoogleApiClient 实例与 Activity 的生命周期有关,并且将实例传递到意图服务。

所以我在想:我怎样才能在不发疯的情况下每五分钟定期更新一次位置?我是否扩展服务,在该组件上实现所有接口回调,在其中构建 GoogleApiClient 实例并使其在后台运行?我是否有一个 AlarmManager 启动一个服务,该服务每五分钟扩展一次 IntentService 来完成工作,再次在 IntentService 中构造所有相关的回调和 GoogleApiClient?我是否继续做我现在正在做的事情,但将 GoogleApiClient 构建为单例,期望它会有所作为?你会怎么做?

感谢和抱歉这么冗长。

4

2 回答 2

9

我目前正在开发一个应用程序,该应用程序必须每五分钟检查一次用户的位置并将坐标发送到服务器。我决定使用 Google Play 服务中的 FusedLocation API,而不是普通的旧 LocationManager API

我们的应用程序具有完全相同的要求,我在几天前实现了它,这就是我的做法。

在启动活动或您想开始的任何地方,使用 AlarmManager 将 LocationTracker 配置为每 5 分钟运行一次。

private void startLocationTracker() {
    // Configure the LocationTracker's broadcast receiver to run every 5 minutes.
    Intent intent = new Intent(this, LocationTracker.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(),
            LocationProvider.FIVE_MINUTES, pendingIntent);
}

LocationTracker.java

public class LocationTracker extends BroadcastReceiver {

    private PowerManager.WakeLock wakeLock;

    @Override
    public void onReceive(Context context, Intent intent) {
        PowerManager pow = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pow.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wakeLock.acquire();

        Location currentLocation = LocationProvider.getInstance().getCurrentLocation();

        // Send new location to backend. // this will be different for you
        UserService.registerLocation(context, new Handlers.OnRegisterLocationRequestCompleteHandler() {
            @Override
            public void onSuccess() {
                Log.d("success", "UserService.RegisterLocation() succeeded");

                wakeLock.release();
            }

            @Override
            public void onFailure(int statusCode, String errorMessage) {
                Log.d("error", "UserService.RegisterLocation() failed");
                Log.d("error", errorMessage);

                wakeLock.release();
            }
        }, currentLocation);
    }
}

LocationProvider.java

public class LocationProvider {

    private static LocationProvider instance = null;
    private static Context context;

    public static final int ONE_MINUTE = 1000 * 60;
    public static final int FIVE_MINUTES = ONE_MINUTE * 5;

    private static Location currentLocation;

    private LocationProvider() {

    }

    public static LocationProvider getInstance() {
        if (instance == null) {
            instance = new LocationProvider();
        }

        return instance;
    }

    public void configureIfNeeded(Context ctx) {
        if (context == null) {
            context = ctx;
            configureLocationUpdates();
        }
    }

    private void configureLocationUpdates() {
        final LocationRequest locationRequest = createLocationRequest();
        final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
                .addApi(LocationServices.API)
                .build();

        googleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
            @Override
            public void onConnected(Bundle bundle) {
                startLocationUpdates(googleApiClient, locationRequest);
            }

            @Override
            public void onConnectionSuspended(int i) {

            }
        });
        googleApiClient.registerConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
            @Override
            public void onConnectionFailed(ConnectionResult connectionResult) {

            }
        });

        googleApiClient.connect();
    }

    private static LocationRequest createLocationRequest() {
        LocationRequest locationRequest = new LocationRequest();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(FIVE_MINUTES);
        return locationRequest;
    }

    private static void startLocationUpdates(GoogleApiClient client, LocationRequest request) {
        LocationServices.FusedLocationApi.requestLocationUpdates(client, request, new com.google.android.gms.location.LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                currentLocation = location;
            }
        });
    }

    public Location getCurrentLocation() {
        return currentLocation;
    }
}

我首先在扩展应用程序的类中创建 LocationProvider 的实例,在启动应用程序时创建实例:

MyApp.java

public class MyApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        LocationProvider locationProvider = LocationProvider.getInstance();
        locationProvider.configureIfNeeded(this);
    }
}

LocationProvider 被实例化并配置为一次位置更新,因为它是一个单例。每 5 分钟它会更新它的currentLocation值,我们可以从任何我们需要的地方检索它

Location loc = LocationProvider.getInstance().getCurrentLocation();

不需要运行任何类型的后台服务。AlarmManager 将每 5 分钟向 LocationTracker.onReceive() 广播一次,部分唤醒锁将确保即使设备处于待机状态,代码也将完成运行。这也是节能的。

请注意,您需要以下权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<!-- For keeping the LocationTracker alive while it is doing networking -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

并且不要忘记注册接收器:

<receiver android:name=".LocationTracker" />
于 2015-10-06T21:19:56.177 回答
0

关于您使用活动请求位置更新的第一种方法,除非您在活动的 onPause() 方法中断开位置客户端,否则它们不应停止。因此,只要您的活动在后台/前台,您就应该继续接收位置更新。但是如果活动被破坏,那么你当然不会得到更新。

检查您是否在活动生命周期中断开位置客户端。

于 2015-10-06T21:40:29.227 回答