[英]Android Geofence triggers on enter/exit with no error but no GEOFENCE_TRANSITION_ flag
我正在使用下面的代碼監視地理圍欄轉換
LocationServices.GeofencingApi
.addGeofences(AssistApplication.getApiHelper().getClient(),
getGeofencingRequest(),
getPendingIntent(context,
GeofenceTransitionIntentService.class))
.setResultCallback(this);
這就是我構建 GeofencingRequest 的方式
private GeofencingRequest getGeofencingRequest()
{
return new GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER |
GeofencingRequest.INITIAL_TRIGGER_EXIT)
.addGeofence(getGeofence())
.build();
}
private Geofence getGeofence()
{
return new Geofence.Builder()
.setRequestId(requestId)
.setCircularRegion(latitude, longitude, 100)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT)
.build();
}
Geofence 在進入和退出時正確觸發,但是當我使用getGeofenceTransition()
時,我沒有獲得三個GEOFENCE_TRANSITION_
標志中的任何一個。
protected void onHandleIntent(Intent intent)
{
final GeofencingEvent event = GeofencingEvent.fromIntent(intent);
if (event.hasError())
{
Log.e(TAG, getErrorMessage(event.getErrorCode()));
return;
}
switch (event.getGeofenceTransition())
{
case Geofence.GEOFENCE_TRANSITION_EXIT:
// Not triggered
break;
case Geofence.GEOFENCE_TRANSITION_ENTER:
case Geofence.GEOFENCE_TRANSITION_DWELL:
// Not triggered
break;
default:
// Triggered on exit and enter
}
}
請告知我在這里缺少什么
從問題中的信息來看,我看不出您的代碼有任何具體問題,但我懷疑您正在處理的Intent
不是來自轉換警報?
我個人使用廣播接收器從 Geofence 接收Intent
,並使用IntentFilter
對其進行過濾。 我使用的結構如下:-
在您的Activity
中聲明廣播接收器
private BroadcastReceiver passedGeofenceReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int geofenceTransition;
// get the Geofencing Event
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
return;
}
// Get the transition type.
geofenceTransition = geofencingEvent.getGeofenceTransition();
if ( geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
// etc etc
在您的Activity
onCreate
方法中注冊此接收器:-
// register this Activity as the receiver of Geofence notifications
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.HIT_GEOFENCE");
this.registerReceiver(this.passedGeofenceReceiver, filter);
在創建廣播PendingIntent
時,設置過濾器(mGeofencePendingIntent 是一個成員PendingIntent
變量):-
private PendingIntent getGeofencePendingIntent() {
// Reuse the PendingIntent if we already have it.
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
Intent hitGeofenceIntent = new Intent("com.example.HIT_GEOFENCE"); //- intent to send a broadcast
mGeofencePendingIntent = PendingIntent.getBroadcast(this, 0, hitGeofenceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
監控您的地理圍欄:-
private void monitorGeofences() {
if ( <check permissions> ) {
GeofencingRequest geofencingRequest = getGeofencingRequest();
PendingIntent pendingIntent = getGeofencePendingIntent();
LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, geofencingRequest, pendingIntent)
.setResultCallback(this); // Result callback fires 'onResult'
}
}
我希望這有幫助。
我在 Android 12 上使用地理圍欄時遇到了同樣的問題。意圖中沒有轉換 (-1)、沒有觸發位置和沒有觸發地理圍欄。
那是因為我在創建掛起意圖時使用了PendingIntent.FLAG_IMMUTABLE
而不是PendingIntent.FLAG_MUTABLE
。
所以在廣播意圖的情況下應該這樣創建:
PendingIntent.getBroadcast(
context, 0,
Intent(context, GeofenceBroadcastReceiver::class.java),
PendingIntent.FLAG_UPDATE_CURRENT or
(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE else 0)
)
@Jukurrpa,我遇到了同樣的問題,即 No transition(-1) 或GEOFENCE_TRANSITION_INVALID_TYPE: -1
我正確地實現了除了PendingIntent
的flags
之外的所有內容。 我正在使用:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
else
PendingIntent.FLAG_UPDATE_CURRENT
您的回答對我幫助很大,過去 4 天我一直在尋找解決方案。 太感謝了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.