I am developing an android application that makes use of new android geofencing API and I want to register more than 100 Geofences.
I'm having an update to my location in the background every 1 minute, and onHandleIntent method in the IntentService class I'm calculating the distance between current location and getting the closest 100 and then I need to monitor these 100 geofences.
The problem is that I am not able to add the closest 100 geofences after getting the current location, and all this have to work when the application is closed.
Here's my code:
package com.example.android.geofence;
import java.util.ArrayList;
import java.util.List;
import android.app.Dialog;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.text.format.DateUtils;
import android.util.Log;
import android.widget.Toast;
import com.example.android.geofence.GeofenceUtils.REMOVE_TYPE;
import com.example.android.geofence.GeofenceUtils.REQUEST_TYPE;
import com.example.android.geofence.MainActivity.GeofenceSampleReceiver;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationClient.OnAddGeofencesResultListener;
public class LocationService extends IntentService implements OnAddGeofencesResultListener, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {
private String TAG = this.getClass().getSimpleName();
public static int notificationID = 0;
LocationClient geoLocationClient;
public String monitoredLounges = "";
public LocationService() {
super("Fused Location");
}
public LocationService(String name) {
super("Fused Location");
}
@Override
public void onCreate() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode == ConnectionResult.SUCCESS) {
geoLocationClient = new LocationClient(this, this, this);
// when I'm using connect() in order to addgeofences my application stops unexpectedly
geoLocationClient.connect();
}
}
@Override
protected void onHandleIntent(Intent intent) {
Location location = intent.getParcelableExtra(LocationClient.KEY_LOCATION_CHANGED);
if(location != null){
RegisterGeofences(location);
Log.i(TAG, "onHandleIntent " + location.getLatitude() + "," + location.getLongitude());
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Builder noti = new NotificationCompat.Builder(this);
noti.setContentTitle("Fused Location");
noti.setContentText(notificationID + "," + location.getLatitude() + "," + location.getLongitude());
noti.setSmallIcon(R.drawable.ic_launcher);
notificationManager.notify(notificationID, noti.build());
notificationID++;
}
}
public void RegisterGeofences(Location _location) {
MySQLiteHelper myDbHelper = new MySQLiteHelper(this);
List<Lounge> closestRegions = myDbHelper.getClosestRegions(_location.getLatitude(), _location.getLongitude());
ArrayList<Geofence> geofenceList = new ArrayList<Geofence>();
int regionIndex = 0;
for (Region cn : closestRegions){
regionIndex++;
if(regionIndex> 100) break;
Geofence geofence = new Geofence.Builder()
.setRequestId(cn.getTitle())
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.setCircularRegion(cn.getLatitude(), cn.getLongitude(), (float) 500.00)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
monitoredLounges += cn.getTitle() + "&";
geofenceList.add(geofence);
}
PendingIntent geoFencePendingIntent = PendingIntent.getService(this, 0,
new Intent(this, GeofenceIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
geoLocationClient.addGeofences(geofenceList, geoFencePendingIntent, this);
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
@Override
public void onAddGeofencesResult(int arg0, String[] arg1) {
// TODO Auto-generated method stub
}
}
Note also that before adding RegisterGeofences Method everything concerning updatelocations was working perfectly in the background