0

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

}
}
Yosem
  • 4,685
  • 3
  • 22
  • 29

2 Answers2

0

I think you can use Intent with putExtra

startService(new Intent(this,MainActivity.class).putExtra("Key","Data));

or CallBack pattern check out this answer

What is callback in Android?

Community
  • 1
  • 1
3zcs
  • 35
  • 4
  • 18
0

I figured out how to complete the task successfully and simply.

The code that I already had for the GeofenceTransitionService is good and the code for MainActivity is good.

In the MainActivity add a variable to the class as follows:

private static MainActivity ins;

and then create two functions within the MainActivity class:

public static MainActivity getInstance(){
    return ins;
}

public void updateMethod(){
    MainActivity.this.runOnUiThread(new Runnable() {
        public void run(){
            //Do what you want to do on the MainActivity here
        }
    });
}

What this does is provide a public method for the GeofenceTransitionService to call and get the MainActivity returned to it and then execute whatever method you want to on the MainActivity. If you did not have the runOnUiThread in place you will get an error at runtime that says you are not allowed to interact with the view from a different thread.

Although not shown here, you can easily add information from the GeofenceTransitionsIntentService and pass to the MainActivity by placing the variables in the updateMethod... For example:

public void updateMethod(final Boolean didEnter){
    // insert the above code here to run on the UI thread
}

Further you can easily create multiple methods that perform various tasks that are all accessible from the intent service.

On the intent service side, the code is also simple:

MainActivity.getInstance().updateMethod([pass stuff here]);

I hope this helps folks to make some more robust geofence apps. I noticed that almost all samples focus on sending notifications and there is so much more that can be done.