I am new to android development and am trying to learn about geofences. I believe I have my intent service working properly where I am able to register my geofence and in theory receive transitions to the service; however, I am interested in taking the data from the intent service and sending it back to MainActivity.class
so that it can be utilized to perform some tasks. I see lots of examples where a notification is created, but I don't want a notification but instead to simply pass the type of transition and the triggering geofence back to the MainActivity class.
My GeofenceTransitionsIntentService.class is below which is where I assume I will need to implement some way to send a result back to the main class and I am assuming the main class will require some sort of listener to receive these results as they post.
public class GeofenceTransitionsIntentService extends IntentService implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
private GoogleApiClient myGoogleApiClient;
public GeofenceTransitionsIntentService(){
super(GeofenceTransitionsIntentService.class.getSimpleName());
}
@Override
public void onCreate(){
super.onCreate();
myGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent geoFenceEvent = GeofencingEvent.fromIntent(intent);
if (geoFenceEvent.hasError()){
int errorcode = geoFenceEvent.getErrorCode();
Log.e("GeofencingApp", "ERROR: " + errorcode);
} else {
int transitionType = geoFenceEvent.getGeofenceTransition();
if (Geofence.GEOFENCE_TRANSITION_ENTER == transitionType){
myGoogleApiClient.blockingConnect(100, TimeUnit.MILLISECONDS);
String triggeredGeofenceID = geoFenceEvent.getTriggeringGeofences().get(0).getRequestId();
} else if (Geofence.GEOFENCE_TRANSITION_EXIT == transitionType) {
myGoogleApiClient.blockingConnect(100, TimeUnit.MILLISECONDS);
String triggeredGeofenceID = geoFenceEvent.getTriggeringGeofences().get(0).getRequestId();
Toast.makeText(getApplicationContext(), "EXIT: " + triggeredGeofenceID, Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}