Геозона Сервис - PullRequest
0 голосов
/ 07 мая 2018

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

Вот что у меня есть:

public class UbicacionEmisor extends Service {

    private static final String TAG = "BOOMBOOMTESTGPS";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 10000;
    private static final float LOCATION_DISTANCE = 0;
    private SOService mService;
    SharedPreferences preferences;
    SharedPreferences.Editor editor;
    private static final String PREF_NAME = "decaught-preferences";
    private List<GeofenceR> geofenceRList = new ArrayList<>();


    private class LocationListener implements android.location.LocationListener {
        Location mLastLocation;

        public LocationListener(String provider) {
            Log.e(TAG, "LocationListener " + provider);
            mLastLocation = new Location(provider);
        }

        @Override
        public void onLocationChanged(Location location) {
            Log.e(TAG, "onLocationChanged: " + location);
            mLastLocation.set(location);

            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
            String date = f.format(new Date());

            editor.putString("latitude",  String.valueOf(location.getLatitude()));
            editor.putString("longitude",  String.valueOf(location.getLongitude()));
            editor.apply();
            //This part is used to send to the server the Location
            mService.add_location("Token " + preferences.getString("token", ""), preferences.getString("imei",""), date, String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()), true).enqueue(new Callback<escoladeltreball.org.decaught.models.Location>() {

                @Override
                public void onResponse(Call<escoladeltreball.org.decaught.models.Location> call, Response<escoladeltreball.org.decaught.models.Location> response) {
                    if (response.isSuccessful()) {
                        Log.d("UbicationEmisor", "Ubication send");
                    } else {
                        int statusCode = response.code();
                        if (response.code() == 400) {
                            try {
                                Log.v("Error code 400", response.errorBody().string());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        Log.d("UbicationEmisor", "Ubication not send, error: " + statusCode);
                        Log.d("UbicationEmisor", "Ubication not send, error: " + response.errorBody());

                        // handle request errors depending on status code
                    }
                }

                @Override
                public void onFailure(Call<escoladeltreball.org.decaught.models.Location> call, Throwable t) {

                    Log.d("RolSelectionActivity", "Error trying to connect to database");

                }
            });

        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.e(TAG, "onProviderDisabled: " + provider);
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.e(TAG, "onProviderEnabled: " + provider);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.e(TAG, "onStatusChanged: " + provider);
        }
    }

    LocationListener[] mLocationListeners = new LocationListener[]{
            new LocationListener(LocationManager.GPS_PROVIDER),
            new LocationListener(LocationManager.NETWORK_PROVIDER)
    };

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");
        super.onStartCommand(intent, flags, startId);
        preferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        editor = preferences.edit();


        return START_STICKY;
    }

    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate");
        mService = ApiUtils.getSOService();
        obtainGeofences();
        initializeLocationManager();
        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                    mLocationListeners[1]);
        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "network provider does not exist, " + ex.getMessage());
        }
        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                    mLocationListeners[0]);
        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "gps provider does not exist " + ex.getMessage());
        }
    }

    private void obtainGeofences() {
        String imei = preferences.getString("imei","");


        mService.listGeofence("Token " + preferences.getString("token", ""), imei).enqueue(new Callback<List<GeofenceR>>() {
            @Override
            public void onResponse(Call<List<GeofenceR>> call, Response<List<GeofenceR>> response) {
               geofenceRList = response.body();
            }

            @Override
            public void onFailure(Call<List<GeofenceR>> call, Throwable t) {

            }
        });
    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy");
        super.onDestroy();
        if (mLocationManager != null) {
            for (int i = 0; i < mLocationListeners.length; i++) {
                try {
                    mLocationManager.removeUpdates(mLocationListeners[i]);
                } catch (Exception ex) {
                    Log.i(TAG, "fail to remove location listners, ignore", ex);
                }
            }
        }
    }

    private void initializeLocationManager() {
        Log.e(TAG, "initializeLocationManager");
        if (mLocationManager == null) {
            mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        }
    }
}

Есть идеи, как сделать геозону частью? Или я должен начать все сначала?

1 Ответ

0 голосов
/ 10 мая 2018

Я работал с Geofences, я действительно предлагаю вам прочитать эту Документацию Geofence и этот пример

Что касается вашего вопроса, я бы посоветовал вам начать все сначала, потому что геозоны не используют LocationManager (или, по крайней мере, явно).

Вы хотите сделать что-то вроде (список шагов):

Определить геозону lng / lat и радиус и назначить событие выхода

mGeofenceList.add(new Geofence.Builder()
    // Set the request ID of the geofence. This is a string to identify this
    // geofence.
    .setRequestId(entry.getKey())

    .setCircularRegion(
            entry.getValue().latitude,
            entry.getValue().longitude,
            Constants.GEOFENCE_RADIUS_IN_METERS
    )
    .setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS)
    .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_EXIT)
    .build());

Определение ожидающего намерения

 public class MainActivity extends AppCompatActivity {

        // ...

        private PendingIntent getGeofencePendingIntent() {
            // Reuse the PendingIntent if we already have it.
            if (mGeofencePendingIntent != null) {
                return mGeofencePendingIntent;
            }
            Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
            // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
            // calling addGeofences() and removeGeofences().
            mGeofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.
                    FLAG_UPDATE_CURRENT);
            return mGeofencePendingIntent;
        }

**Define the Service**

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

            // Get the geofences that were triggered. A single event can trigger
            // multiple geofences.


            // HERE YOUR CODE TO SAVE THE LOCATION IN THE DB
            // AND SEND THE NOTIFICATION 

            sendNotification(geofenceTransitionDetails);
            Log.i(TAG, geofenceTransitionDetails);
        } else {
            // Log the error.
            Log.e(TAG, getString(R.string.geofence_transition_invalid_type,
                    geofenceTransition));
        }
    }

NB

Самым раздражающим при работе с геозонами является Doze . Через некоторое время вы больше не увидите никаких уведомлений, потому что Doze остановил его, чтобы сэкономить заряд батареи, так что имейте это в виду. Обязательно прочтите это, прежде чем начать DOZE .

Наслаждайтесь =)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...