0

I want to add a geofence when the app recieves a gcm push, but I can not instantiate the GeofenceRequester because it needs to have an activity parameter. I have googled for a while and can't figure out how to instantiate it correctly.

When I run this code I get the following error:

Error:(68, 52) error: incompatible types: GcmMessageHandler cannot be converted to Activity

My code in the IntentService looks so far:

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);

    Double lat = Double.parseDouble(intent.getStringExtra("lat"));
    Double lng = Double.parseDouble(intent.getStringExtra("lng"));
    Float radius = Float.parseFloat("500");
    String id = intent.getStringExtra("id");

    addGeofence(lat, lng, radius);

    //showToast();

    Log.i("Data", "lat:" + lat + " lon:" + lng + " id:" + id);

    GcmReceiver.completeWakefulIntent(intent);
}


private void addGeofence(Double lat, Double lng, Float radius) {

    mGeofenceRequester = new GeofenceRequester(this);

    mRequestType = GeofenceUtils.REQUEST_TYPE.ADD;
    SimpleGeofence mGeofence = new SimpleGeofence(
            "1",
            lat,
            lng,
            radius,
            GEOFENCE_EXPIRATION_IN_MILLISECONDS,
            Geofence.GEOFENCE_TRANSITION_ENTER);

    mCurrentGeofences.add(mGeofence.toGeofence());

    try {
        mGeofenceRequester.addGeofences(mCurrentGeofences);
    } catch (UnsupportedOperationException e) {
        Toast.makeText(this, R.string.add_geofences_already_requested_error,
                Toast.LENGTH_LONG).show();
    }

}

Any ideas?

EDIT:

The GeofenceRequester class uses the activity parameter as following:

public class GeofenceRequester
                implements
                    OnAddGeofencesResultListener,
                    ConnectionCallbacks,
                    OnConnectionFailedListener {
   ...

   // Storage for a reference to the calling client
   private final Activity mActivity;

   ...

   public GeofenceRequester(Activity activityContext) {
      // Save the context
      mActivity = activityContext;

      ...
   }

   ...

   private GooglePlayServicesClient getLocationClient() {
      if (mLocationClient == null) {
          mLocationClient = new LocationClient(mActivity, this, this);
      }
      return mLocationClient;
   }

   ...

   // Later in another function
   msg = mActivity.getString(R.string.add_geofences_result_success,
         Arrays.toString(geofenceRequestIds));
}
BastianW
  • 270
  • 2
  • 17
  • Which line is the error on in your code? I'm guessing you need to pass it the activity in which it's running, so either use 'this' if you're in an activity class (although I suspect this isn't the case), or pass a reference to the activity when you instantiate the GeofenceRequester. Your best bet is to look at the docs for GeofenceRequester and see what it needs the activity for, then you'll know what you need to pass in and why – Matt Taylor Dec 18 '14 at 08:32
  • The error is in the line right after defining the addGeofence function. mGeofenceRequester = new GeofenceRequester(this); – BastianW Dec 18 '14 at 08:35
  • So you're trying to instantiate it in your GcmMessageHandler class, which doesn't inherit 'Activity', meaning it won't work. Read all of my previous comment, then give that a go – Matt Taylor Dec 18 '14 at 08:38
  • Okay, I try to understand what you mean. Meanwhile I have edited my post with the GeofenceRequester class to give you information, how the acitivity parameter is used. – BastianW Dec 18 '14 at 08:44
  • I don't understand how to pass a reference..googled it but the only thing I get is headache..is there no easy way to do that? I found a similar post, but no answer to GeofenceRequester: http://stackoverflow.com/questions/22392732/create-android-geofence-in-background-without-crashing?rq=1 – BastianW Dec 18 '14 at 09:14
  • As in call addGeofence(Double lat, Double lng, Float radius, Activity callingActivity); - passing Activity in as a parameter – Matt Taylor Dec 18 '14 at 09:48
  • okay, so far so good...but I still don't have any activity to pass..it can't be null. – BastianW Dec 18 '14 at 11:41
  • take a look at http://stackoverflow.com/questions/19434999/android-geofence-only-works-with-opened-app?rq=1, as I think you're heading towards their solution – Matt Taylor Dec 18 '14 at 12:19
  • I guess I am dumb..I am new to Java, coming from Webdev..I really cannot figure out how to combine those two things..as I read, you don't have a clue either, but thanks anyway. – BastianW Dec 18 '14 at 14:33

0 Answers0