Android Geofence exit Intent 不包含任何数据



我在Google的Play服务中使用相对较新版本的Geofence API。问题(除了过时的文档(是收到的意图(在地理围栏退出后(似乎不包含任何数据。

  • 我自己添加一个类别,并且已成功检索到此类别。
  • 我也添加了一个可序列化的额外内容,它可以完美地从意图中检索到。

上述两个论点证明,我收到的意图实际上是我为地理围栏过渡安排的意图。

什么不起作用。

地理围栏事件似乎返回所有方法的默认值。所以我得到;

  • false for hasError((
  • 和 -1 表示 getErrorCode((
  • getGeofenceTransition(( 返回 -1
  • getTriggeredGeofences(( 返回的列表为空

这似乎与 API 文档相矛盾...getGeofenceTransition((;

-1 如果未为转换警报生成 fromIntent(Intent( 中指定的意图;否则返回在地理围栏中定义的GEOFENCE_TRANSITION_标志值。

使用 getErrorCode((;

在 GeofenceStatusCodes 中指定的错误代码,如果 hasError(( 返回 false,则为 -1。

和 hasError((;

如果错误触发了 fromIntent(Intent( 中指定的意图,则为 true,否则为 false

如何添加地理围栏

// create fence
Geofence fence = new Geofence.Builder()
        .setRequestId(an_id)
        .setCircularRegion(lat, long, radius)
        .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_EXIT)
        .setExpirationDuration(240 * 60 * 1000)
        .build();
// create pendingintent
Intent launch = new Intent(this, same_class.class);
launch.putExtra(extra_object_key, extra_object);
launch.addCategory(category_name);
PendingIntent intent = PendingIntent.getService(
        getApplicationContext(),
        0, launch,
        PendingIntent.FLAG_UPDATE_CURRENT
);
// create add fence request
GeofencingRequest request = new GeofencingRequest.Builder()
        .addGeofence(fence)
        .build();
// add fence request
PendingResult<Status> result = LocationServices.GeofencingApi.addGeofences(googleApiClient,
        request, intent);
// we'll do this synchronous
Status status = result.await();
if (status.getStatusCode() != GeofenceStatusCodes.SUCCESS)
{
    Log.e(SERVICE_NAME, "Failed to add a geofence; " + status.getStatusMessage());
    return false;
}

我如何接收意向

protected void onHandleIntent(Intent intent)
{
    if (intent.hasCategory(category_name))
    {
        GeofencingEvent event = GeofencingEvent.fromIntent(intent);
        if (event.hasError())
        {
            Log.e(SERVICE_NAME, "Failed geofence intent, code: " + event.getErrorCode());
        }
        else
        {
            // checked 'event' here with debugger and prints for the stuff I wrote above
            switch (event.getGeofenceTransition())
            {
                case Geofence.GEOFENCE_TRANSITION_EXIT:
                    // NEVER REACHED
                    type extra_object =   (type)intent.getSerializableExtra(extra_object_key);
                    onGeofenceExit(extra_object);
                    break;
                default:
                    // ALWAYS END UP HERE
                    throw new AssertionError("Geofence events we didn't register for.");
            }
        }
    }
    else
    {
        throw new AssertionError("Received unexpected intent.");
    }
}

连接到游戏服务

public void onCreate()
{
    super.onCreate();
    googleApiAvailable = false;
    GoogleApiClient.Builder  gApiBuilder = new GoogleApiClient.Builder(getApplicationContext());
    gApiBuilder.addApi(LocationServices.API);
    gApiBuilder.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks()
    {
        @Override
        public void onConnected(Bundle bundle)
        {
            googleApiAvailable = true;
        }
        @Override
        public void onConnectionSuspended(int i)
        {
            googleApiAvailable = false;
        }
    });
    gApiBuilder.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener()
    {
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult)
        {
            googleApiAvailable = false;
        }
    });
    googleApiClient = gApiBuilder.build();
}

你在哪里连接到googleApiClient?应该有

googleApiClient.connect();

并等待已连接或

googleApiClient.blockingConnect();

在你的代码中,但我看不到它。发布整个代码。

最新更新