地理围栏在更新的Google Play服务中无效

时间:2017-09-14 10:40:16

标签: android google-maps android-geofence

我正在研究GeoFence,我在这里遇到的问题是使用更新的Google服务依赖 com.google.android.gms:播放服务:7.3.0 地理围栏消息无法正常工作并尝试了stackoverflow中提到的可能的方法,但它没有工作。这是代码,并建议我,如果它的错误。提前谢谢。

  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 个答案:

答案 0 :(得分:0)

您的依赖项将加载所有播放服务库,这不是正确的实现方式。你应该特别玩服务库。

不要使用:

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

使用:

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

如果您使用地图。使用以下依赖项加载地图库。

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