如何在提供商上重新注册android geofences?

时间:2016-06-21 06:14:06

标签: android broadcastreceiver google-api-client geofencing android-geofence

在提供商更改后,我的地理围栏停止工作,否则其他一切正常。 这是我的广播接收器代码,我尝试重新注册我的地理围栏。

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

public class LocationProviderChangedReceiver extends BroadcastReceiver implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,ResultCallback<Status> {
    int i=0;
    protected GoogleApiClient mGoogleApiClient;
    Geofence geofence;
    Context context;

    @Override
    public void onReceive(Context context, Intent intent) {
        i=i++;
        this.context=context;
        Toast.makeText(context,"Provider changed ,Geofencing restarted.",Toast.LENGTH_LONG).show();
        Log.wtf("senpai","oooooo"+i);
        LatLng latLng=new LatLng(28.5795613,77.3136267);

        geofence=new Geofence.Builder()
                .setRequestId("x").setCircularRegion(latLng.latitude,latLng.longitude, Constants.GEOFENCE_RADIUS_IN_METERS)
                .setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS)
                .setLoiteringDelay(500)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT|Geofence.GEOFENCE_TRANSITION_DWELL)
                .build();

        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        try {
            LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, getGeofencePendingIntent()).setResultCallback(this); // Result processed in onResult().
            LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, getGeofencingRequest(), getGeofencePendingIntent()).setResultCallback(this); // Result processed in onResult().
        } catch (SecurityException securityException) {
            // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    @Override
    public void onResult(@NonNull Status status) {

    }

    private PendingIntent getGeofencePendingIntent() {
        Intent intent = new Intent("ACTION_RECEIVE_GEOFENCE");
        // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling addgeoFences()
        return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    private GeofencingRequest getGeofencingRequest() {
        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
        builder.addGeofence(geofence);
        return builder.build();
    }
}

在我的活动中,我也做了同样的事情。如何重新启动地理围栏?

1 个答案:

答案 0 :(得分:1)

为了保持地理围栏的运行,您必须在(a)位置提供商更改以及(b)设备重启上重新注册您的地理围栏:

以下是我工作的Geofencer示例中的一些代码段,其中测试Google Play Geofencing并将其与其他库进行比较:

在清单中为两个事件注册广播接收器:

    <!-- Listen for the device starting up for re-registration tasks -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
    <receiver
        android:name=".geo.GeoRestartReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />

            <action android:name="android.intent.action.BOOT_COMPLETED" /> <!-- android.intent.action.LOCKED_BOOT_COMPLETED would be triggered earlier, in case we don't need access to credential secure storage -->
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
            <action android:name="android.location.PROVIDERS_CHANGED" />
        </intent-filter>
    </receiver>

创建一个(重新)启动GeoFencing的接收器(通过重新创建地理围栏请求:

public class GeoRestartReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (!GeoLocationUtil.isGpsEnabled(context) || !GeoLocationUtil.hasGpsPermissions(context))
            return;

        GeofenceSettings settings = new GeofenceSettings(context);
        if (!settings.isGeofencingActive()) {
            return;
        }

        GeofenceProvider geofenceProvider = new PlayGeofenceProvider(context);
        geofenceProvider.start(homeLocation, enterRadius, exitRadius,
            initTrigger, usePolling);
    }
}

大多数Google-Geofencing API逻辑都封装在我的自定义类中。只需查看Geofencer回购以查看更多详细信息。