7

我已经开始在 Android 上使用最后一个位置服务功能进行开发:地理围栏!模拟位置提供程序是否存在任何已知问题?以下示例(https://developer.android.com/training/location/geofencing.html)即使当前位置在地理围栏内,我的意图服务也从未触发。我正在使用 FakeGPS android 应用程序作为模拟位置提供程序,如果我模拟路线,我会在 Google 地图应用程序上看到位置变化,因此模拟位置提供程序运行良好。有任何想法吗 ?

谢谢。保罗。

4

6 回答 6

4

我一直试图让它发挥作用。谷歌多么痛苦!因为它说可以使用模拟轻松测试地理围栏。

魔术技巧是在传递给 setMockLocation 的位置中使用提供者名称“网络”。

    Location location = new Location("network");
    location.setLatitude(latitude);
    location.setLongitude(longitude);
    location.setTime(new Date().getTime());
    location.setAccuracy(3.0f);
    location.setElapsedRealtimeNanos(System.nanoTime());

    LocationServices.FusedLocationApi.setMockLocation(_googleApiClient, location);
于 2014-09-29T21:21:02.873 回答
2

实际上,如果您的应用程序在前台,则在上述示例中使用的 Intent 服务效果很好,但是当应用程序在后台时,该 IntentService 永远不会被调用。因此我们需要使用 Broadcast-Receiver 而不是 Intent 服务。

我发现这个博客有助于获得解决方案。

http://davehiren.blogspot.in/2015/01/android-geofence-stop-getting.html

于 2017-04-03T09:37:33.943 回答
1

地理围栏使用 FusedLocationProviderApi 所以要模拟它们你必须使用FusedLocationProviderApi.setMockLocation

于 2015-02-04T13:56:36.470 回答
0

在设置 Mock Location 之前需要使用 LocationServices.FusedLocationApi.setMockMode(googleApiClient, true)。

于 2015-02-28T10:36:33.263 回答
0

确保在您的手机上启用模拟位置。选择设置->开发者选项->“允许模拟位置”。

于 2015-01-23T22:42:46.863 回答
0

您可以使用广播接收器而不是这样的活动

public class GeofenceReceiver extends BroadcastReceiver
implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    ResultCallback<Status>{

GoogleApiClient mGoogleApiClient;
PendingIntent mGeofencePendingIntent ;
Context mContext;

@Override
public void onReceive(Context context, Intent intent) {
    mContext = context;
    mGoogleApiClient = new GoogleApiClient.Builder(mContext)
            .addOnConnectionFailedListener(this)
            .addConnectionCallbacks(this)
            .addApi(LocationServices.API)
            .build();

    mGoogleApiClient.connect();
}



@Override
public void onConnected(@Nullable Bundle bundle) {
    try {
        LocationServices.GeofencingApi.addGeofences(
                mGoogleApiClient,
                // The GeofenceRequest object.
                getGeofencingRequest(),
                getGeofencePendingIntent()
        ).setResultCallback(this); // Result processed in onResult().
    } catch (SecurityException securityException) {
        Log.i(getClass().getSimpleName(),securityException.getMessage());
    }
}

// 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) {

}

/**
 * Runs when the result of calling addGeofences() and removeGeofences() becomes available.
 * Either method can complete successfully or with an error.
 *
 * Since this activity implements the {@link ResultCallback} interface, we are required to
 * define this method.
 *
 * @param status The Status returned through a PendingIntent when addGeofences() or
 *               removeGeofences() get called.
 */
@Override
public void onResult(@NonNull Status status) {
    if (status.isSuccess()) {
        Log.i(getClass().getSimpleName(),"Success");
    } else {
        // Get the status code for the error and log it using a user-friendly message.
        Log.i(getClass().getSimpleName(),getErrorString(status.getStatusCode()));
    }
}

private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_DWELL);
    builder.addGeofences(getGeofecne());
    return builder.build();
}

private List<Geofence> getGeofecne(){
    List<Geofence> mGeofenceList = new ArrayList<>();

    //add one object
    mGeofenceList.add(new Geofence.Builder()
            // Set the request ID of the geofence. This is a string to identify this
            // geofence.
            .setRequestId("key")

            // Set the circular region of this geofence.
            .setCircularRegion(
                    25.768466, //lat
                    47.567625, //long
                    50) // radios

            // Set the expiration duration of the geofence. This geofence gets automatically
            // removed after this period of time.
            //1000 millis  * 60 sec * 5 min
            .setExpirationDuration(1000 * 60 * 5)

            // Set the transition types of interest. Alerts are only generated for these
            // transition. We track entry and exit transitions in this sample.
            .setTransitionTypes(
                    Geofence.GEOFENCE_TRANSITION_DWELL)
            //it's must to set time in millis with dwell transition
            .setLoiteringDelay(3000)
            // Create the geofence.
            .build());

    return mGeofenceList;

}

private PendingIntent getGeofencePendingIntent() {
    // Reuse the PendingIntent if we already have it.
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }
    Intent intent = new Intent(mContext, GeofenceTransitionsIntentService.class);
    return PendingIntent.getService(mContext, 0, intent, PendingIntent.
            FLAG_UPDATE_CURRENT);
}

}

查看我的仓库,有一个使用地理围栏的完整示例 https://github.com/3zcs/Geofence

于 2017-04-03T20:03:53.897 回答