![](/img/trans.png)
[英]Android Geofence triggers on enter/exit with no error but no GEOFENCE_TRANSITION_ flag
[英]Android Geofence exit intent contains no data
我在Google的Play服務中使用了相對較新版本的Geofence API。 問題(除了過時的文檔)是(在地理圍欄退出之后)收到的Intents似乎不包含任何數據。
以上兩個論據證明,我收到的意圖實際上是我計划進行地理圍欄轉換的意圖。
GeofenceEvent似乎返回所有方法的默認值。 所以我得到了;
這似乎與API文檔矛盾... getGeofenceTransition();
如果未為過渡警報生成fromIntent(Intent)中指定的意圖,則為-1;否則為-1。 否則,返回在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();
}
您在哪里連接到googleApiClient
? 應該有
googleApiClient.connect();
然后等待onConnected或
googleApiClient.blockingConnect();
在您的代碼中,但我看不到它。 發布整個代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.