Android регулярно обновляет геозону - PullRequest
0 голосов
/ 15 ноября 2018

Я читаю документацию отсюда.

https://developer.android.com/training/location/geofencing

Он может обновлять мое местоположение для геозоны, когда я запускаю этот сервис. Проблема в том, что мне нужно постоянно следить. Итак, я думаю сделать таймер на своем сервисе и регулярно проверять свою геозону Но, думаю, это точно разрядит батарею.

Я не хочу отслеживать местоположение, и вместо этого мне нужна ОС Android, чтобы обновить меня, входит ли пользователь в мою геозону, как iOS. Есть ли способ реализовать это?

public class GeofenceTransitionsIntentService extends IntentService {
    // ...
    protected void onHandleIntent(Intent intent) {
        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
        if (geofencingEvent.hasError()) {
            String errorMessage = GeofenceErrorMessages.getErrorString(this,
                    geofencingEvent.getErrorCode());
            Log.e(TAG, errorMessage);
            return;
        }

        // Get the transition type.
        int geofenceTransition = geofencingEvent.getGeofenceTransition();

        // Test that the reported transition was of interest.
        if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
                geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

            // Get the geofences that were triggered. A single event can trigger
            // multiple geofences.
            List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

            // Get the transition details as a String.
            String geofenceTransitionDetails = getGeofenceTransitionDetails(
                    this,
                    geofenceTransition,
                    triggeringGeofences
            );

            // Send notification and log the transition details.
            sendNotification(geofenceTransitionDetails);
            Log.i(TAG, geofenceTransitionDetails);
        } else {
            // Log the error.
            Log.e(TAG, getString(R.string.geofence_transition_invalid_type,
                    geofenceTransition));
        }
    }
...