【发布时间】: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