Geofence is not working in updated Google play service

416 Views Asked by At

I am working on GeoFence and the issue which i am facing here is while using updated google service dependency com.google.android.gms:play-services:7.3.0 geofence message is not working and tried the possible method mentioned in stackoverflow but its not working.Here is the code and suggest me if its wrong.Thanks in advance.

  public class Geofencing  implements  GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

private List<GeoRegions> geoRegions;
private GoogleApiClient locationClient;
private Context curr_con;
private boolean location_settings = false;
private static final String TAG = Geofencing.class.getSimpleName();
private Context mContext;
public Geofencing(Context ctx, boolean settings) {
    this.curr_con = ctx;
    this.location_settings = settings;
}

public void startGeo() {
    printedCardNumber = AppUtil.sPxAPI.getTokenInfo()
            .getPrintedCardNumber();
    locationClient = new GoogleApiClient.Builder(mContext)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    new GetPartSections().execute();
}
private void run_geofence() {
    ArrayList<Geofence> mGeoList1 = new ArrayList<Geofence>();

    for (GeoRegions georegion : geoRegions) {
        Coordinates co = georegion.getCoordinates();

        float radius = (float) georegion.getRadius();
        Geofence geofence = new Geofence.Builder()
                .setRequestId(georegion.getStoreCode())
                .setTransitionTypes(
                        Geofence.GEOFENCE_TRANSITION_ENTER
                                | Geofence.GEOFENCE_TRANSITION_EXIT)
                .setCircularRegion(co.getLatitude(), co.getLongitude(),
                        radius)
                .setExpirationDuration(Geofence.NEVER_EXPIRE).build();
        mGeoList1.add(geofence);
    }
    if (mGeoList1 != null && mGeoList1.size() > 0) {
        Intent pIntent = new Intent(curr_con, GeofenceIntentService.class);
        pIntent.putExtra("assemble", code);
        PendingIntent geoFencePendingIntent = PendingIntent.getService(
                curr_con, 0, pIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        LocationServices.GeofencingApi.addGeofences(
                locationClient,
                mGeoList1,
                geoFencePendingIntent
        ).setResultCallback((ResultCallback<Status>) Geofencing.this);

    }
}

@Override
public void onLocationChanged(Location location) {

}


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.e(TAG + ".LocationClientConnectiond", "" + connectionResult);
}

@Override
public void onConnected(Bundle bundle) {

    if (geoRegions != null && geoRegions.size() > 0) {
        run_geofence();
    }
}
   @Override
public void onConnectionSuspended(int i) {

}

}



android {
compileSdkVersion 24
buildToolsVersion "25.0.3"

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    }
}

dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:support-v4:24.2.1'
compile "fr.avianey.com.viewpagerindicator:library:2.4.1.1@aar"
compile "com.android.support:recyclerview-v7:24.2.1"
compile "org.twitter4j:twitter4j-core:4.0.2"
compile "com.google.zxing:core:2.0"
compile "com.google.android.gms:play-services:7.3.0"
compile "com.facebook.android:facebook-android-sdk:4.0.1"
testCompile 'junit:junit:4.12'
}
1

There are 1 best solutions below

2
On

Your dependency will load all the play services library it is not a correct way of implementation. You should you particular play service library.

Don't use:

com.google.android.gms:play-services:Your_version

Use:

compile 'com.google.android.gms:play-services-location:Your_Version'

If you use map. load map library using below dependency.

compile 'com.google.android.gms:play-services-maps:Your_Version'