首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用GeofencingClient时不工作退出转换的地理位置

使用GeofencingClient时不工作退出转换的地理位置
EN

Stack Overflow用户
提问于 2017-11-01 03:31:09
回答 1查看 772关注 0票数 0

我正在尝试设置一个Geofence,以便在用户退出围绕一个BroadcastReceiver的100m Geofence时,在Location上得到一个回调。这就是我所实施的。

代码语言:javascript
运行
复制
public class GeofenceTask implements OnCompleteListener<Void> {
public static final String LAST_STILL_LOCATION_GEO_FENCE_ID = "LastStillLocationGeoFence";
private Context context;

public GeofenceTask(Context context) {
    this.context = context;
}

@SuppressLint("MissingPermission")
public void setupGeoFence(double latitude, double longitude) {
    Timber.e("Setting up Geofence on Lat: " + latitude + ", Lon :" + longitude);
    GeofencingClient client = LocationServices.getGeofencingClient(context);
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_EXIT);

    // Add the geofences to be monitored by geofencing service.
    builder.addGeofence(createGeoFencingRequest(latitude, longitude));

    // Return a GeofencingRequest.
    GeofencingRequest geofencingRequest = builder.build();
    client.addGeofences(geofencingRequest, getGeofencePendingIntent()).addOnCompleteListener(this);

}

public void setupGeoFence(Location location) {
    setupGeoFence(location.getLatitude(), location.getLongitude());
}

public void removeAnyGeoFences() {
    GeofencingClient client = LocationServices.getGeofencingClient(context);
    client.removeGeofences(getGeofencePendingIntent()).addOnCompleteListener(this);

}

private PendingIntent getGeofencePendingIntent() {
    Intent intent = new Intent(context, GeofenceTransitionReceiver.class);
    intent.setAction(GeofenceTransitionReceiver.ACTION_GEOFENCE_TRANSITION);
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

private Geofence createGeoFencingRequest(double latitude, double longitude) {
    return new Geofence.Builder()
            .setRequestId(LAST_STILL_LOCATION_GEO_FENCE_ID)
            .setCircularRegion(latitude, longitude,100)
            .setExpirationDuration(Geofence.NEVER_EXPIRE)
            .setNotificationResponsiveness(0)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_EXIT)
            .build();
}
@Override
public void onComplete(@NonNull Task<Void> task) {
    if (task.isSuccessful()) {
        Timber.i("Geofence Task onComplete");
    } else {
        // Get the status code for the error and log it using a user-friendly message.
        String errorMessage = GeofenceErrorMessages.getErrorString(context, task.getException());
        Timber.e(errorMessage);
    }
}

BroadcastReceiver代码

代码语言:javascript
运行
复制
public class GeofenceTransitionReceiver extends BroadcastReceiver {

static final String ACTION_GEOFENCE_TRANSITION = PACKAGE_NAME + ".ACTION_GEOFENCE_TRANSITION";


@Override
public void onReceive(Context context, Intent intent) {
    Timber.i("onReceive");
    if (intent != null) {
        final String action = intent.getAction();
        if (ACTION_GEOFENCE_TRANSITION.equals(action)) {
            GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
            if (geofencingEvent.hasError()) {
                String errorMessage = GeofenceErrorMessages.getErrorString(context, geofencingEvent.getErrorCode());
                Timber.e(errorMessage);
                return;
            }

            // Get the transition type.
            int geofenceTransition = geofencingEvent.getGeofenceTransition();

            // Test that the reported transition was of interest.
            if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

                Location newLocation = geofencingEvent.getTriggeringLocation();
                Timber.i("Triggering Location " + newLocation.toString() + "!!!!!!");

                // Get the geofences that were triggered. A single event can trigger multiple geofences.
                List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
                Timber.i("--------Triggering GeoFences Start------");
                for (Geofence geofence : triggeringGeofences) {
                    Timber.i(geofence.toString());
                }
                Timber.i("--------Triggering GeoFences End ------");
            } else {
                // Log the error.
                Timber.e(context.getString(R.string.geofence_transition_invalid_type, geofenceTransition));
            }
        } else {
            Timber.w("Invalid Action");
        }
    } else {
        Timber.w("Intent Null");
    }
}

在仿真器中测试时,我给了这个应用程序位置许可。我就是这样宣布我的BroadcastReceiver的。

代码语言:javascript
运行
复制
 <receiver
        android:name=".location.geofence.GeofenceTransitionReceiver"
        android:exported="true">
    </receiver>

然后我打电话:

代码语言:javascript
运行
复制
 GeofenceTask geofenceTask = new GeofenceTask(this);
 geofenceTask.setupGeoFence(37.7042, -122.471);

现在在模拟器中,当我发送位置为纬度- 37.7051和经度-122.47时。我没有接到GeofenceTransitionReceiver的电话。

EN

Stack Overflow用户

回答已采纳

发布于 2017-11-01 23:40:22

原来我的代码起作用了。只是在仿真器中,地理位置不起作用,因为地理位置设置使用网络/wifi信息来触发Geofence转换。当我在物理设备上测试时,我的代码起作用了。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47047795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档