【问题标题】:Android Geofence exit intent contains no dataAndroid Geofence 退出意图不包含任何数据
【发布时间】:2014-12-11 13:39:29
【问题描述】:

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

  • 我自己添加了一个分类,成功检索到这个分类。
  • 我还添加了一个可序列化的额外内容,可以完美地从意图中检索到。

以上两个论点证明我收到的意图实际上是我为地理围栏转换安排的意图。

什么不起作用。

GeofenceEvent 似乎返回所有方法的默认值。所以我明白了;

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

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

-1 如果在 fromIntent(Intent) 中指定的意图不是为转换警报生成的;否则返回 Geofence 中定义的 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();
}

【问题讨论】:

  • 不,我从来没有这样做过。如果你有消息我仍然感兴趣!
  • 我有同样的问题,当我通过额外的 getGeofenceTransition 传递可序列化对象时返回-1,但是当我传递一些原始变量时它可以工作。 . .

标签: android google-play-services geofencing android-geofence


【解决方案1】:

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

googleApiClient.connect();

然后等待onConnected

googleApiClient.blockingConnect();

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

【讨论】:

  • 我添加了用于设置播放服务的代码。在我做任何地理围栏之前,我使用了 blockingConnect() 方法并检查了布尔值。
  • 它就在那里,我的班级比这里发布的要大得多,我不想在这里复制粘贴一大块不相关的细节。整个事情是一个意图服务,并且 onHandleIntent 具有不同类别的“句柄”。添加围栏的类别也有连接。如您所见,我还在添加围栏后检查错误。这不会出现错误。
猜你喜欢
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
  • 2010-10-16
  • 1970-01-01
  • 2023-03-23
  • 2019-10-12
  • 2012-08-06
  • 2021-04-02
相关资源
最近更新 更多