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));
}