Я пытаюсь создать службу, которая вставляет местоположение устройства в базу данных, и когда устройство находится вне списка геозон, оно отправляет на сервер другое сообщение. Прямо сейчас у меня есть только часть, где служба отправляет местоположение на сервер.
Вот что у меня есть:
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);
}
}
}
Есть идеи, как сделать геозону частью? Или я должен начать все сначала?