0

Geofenceを設定して、ブロードキャスト受信者が100mを終了したときにブロードキャスト受信者がLocationを取り囲むようにコールバックを取得しようとしています。これが私が実装したものです。エミュレータでテストするとき、私はアプリに場所の許可を与えているBroadcastReceiverGeofencingClientを使用しているときに終了トランジションでジオフェンシングが機能しない

public class GeofenceTransitionReceiver extends BroadcastReceiver { 

static final String ACTION_GEOFENCE_TRANSITION = PACKAGE_NAME + ".ACTION_GEOFENCE_TRANSITION"; 


@Override 
public void onReceive(Context context, Intent intent) { 
    Timber.i("onReceive"); 
    if (intent != null) { 
     final String action = intent.getAction(); 
     if (ACTION_GEOFENCE_TRANSITION.equals(action)) { 
      GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); 
      if (geofencingEvent.hasError()) { 
       String errorMessage = GeofenceErrorMessages.getErrorString(context, geofencingEvent.getErrorCode()); 
       Timber.e(errorMessage); 
       return; 
      } 

      // Get the transition type. 
      int geofenceTransition = geofencingEvent.getGeofenceTransition(); 

      // Test that the reported transition was of interest. 
      if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { 

       Location newLocation = geofencingEvent.getTriggeringLocation(); 
       Timber.i("Triggering Location " + newLocation.toString() + "!!!!!!"); 

       // Get the geofences that were triggered. A single event can trigger multiple geofences. 
       List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences(); 
       Timber.i("--------Triggering GeoFences Start------"); 
       for (Geofence geofence : triggeringGeofences) { 
        Timber.i(geofence.toString()); 
       } 
       Timber.i("--------Triggering GeoFences End ------"); 
      } else { 
       // Log the error. 
       Timber.e(context.getString(R.string.geofence_transition_invalid_type, geofenceTransition)); 
      } 
     } else { 
      Timber.w("Invalid Action"); 
     } 
    } else { 
     Timber.w("Intent Null"); 
    } 
} 

ため

public class GeofenceTask implements OnCompleteListener<Void> { 
public static final String LAST_STILL_LOCATION_GEO_FENCE_ID = "LastStillLocationGeoFence"; 
private Context context; 

public GeofenceTask(Context context) { 
    this.context = context; 
} 

@SuppressLint("MissingPermission") 
public void setupGeoFence(double latitude, double longitude) { 
    Timber.e("Setting up Geofence on Lat: " + latitude + ", Lon :" + longitude); 
    GeofencingClient client = LocationServices.getGeofencingClient(context); 
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); 
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_EXIT); 

    // Add the geofences to be monitored by geofencing service. 
    builder.addGeofence(createGeoFencingRequest(latitude, longitude)); 

    // Return a GeofencingRequest. 
    GeofencingRequest geofencingRequest = builder.build(); 
    client.addGeofences(geofencingRequest, getGeofencePendingIntent()).addOnCompleteListener(this); 

} 

public void setupGeoFence(Location location) { 
    setupGeoFence(location.getLatitude(), location.getLongitude()); 
} 

public void removeAnyGeoFences() { 
    GeofencingClient client = LocationServices.getGeofencingClient(context); 
    client.removeGeofences(getGeofencePendingIntent()).addOnCompleteListener(this); 

} 

private PendingIntent getGeofencePendingIntent() { 
    Intent intent = new Intent(context, GeofenceTransitionReceiver.class); 
    intent.setAction(GeofenceTransitionReceiver.ACTION_GEOFENCE_TRANSITION); 
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 

private Geofence createGeoFencingRequest(double latitude, double longitude) { 
    return new Geofence.Builder() 
      .setRequestId(LAST_STILL_LOCATION_GEO_FENCE_ID) 
      .setCircularRegion(latitude, longitude,100) 
      .setExpirationDuration(Geofence.NEVER_EXPIRE) 
      .setNotificationResponsiveness(0) 
      .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_EXIT) 
      .build(); 
} 
@Override 
public void onComplete(@NonNull Task<Void> task) { 
    if (task.isSuccessful()) { 
     Timber.i("Geofence Task onComplete"); 
    } else { 
     // Get the status code for the error and log it using a user-friendly message. 
     String errorMessage = GeofenceErrorMessages.getErrorString(context, task.getException()); 
     Timber.e(errorMessage); 
    } 
} 

コード。これは私がBroadcastReceiverと宣言した方法です。

<receiver 
     android:name=".location.geofence.GeofenceTransitionReceiver" 
     android:exported="true"> 
    </receiver> 

は、それから私は呼ん:

今エミュレータで、私は緯度と場所を送信するとき
GeofenceTask geofenceTask = new GeofenceTask(this); 
geofenceTask.setupGeoFence(37.7042, -122.471); 

から37.7051と経度 - -122.47。私はGeofenceTransitionReceiverに電話をしません。

+0

どのように1つの場所を終了することができますか?あなたは以前の場所について何も教えていませんでした。 – greenapps

+0

私は 'GeofenceTask'をセットアップするとき、'(37.7042、-122.471) 'の場所を指定します。 133メートル先の次の場所 '(37.7051、-122.47)'に行くと、Geofence Exitの遷移が開始されます。右? – NinjaCoder

+0

ジオフェンスを設定することは、フェンス内に置くことと同じではありません。そうではありません。 – greenapps

答えて

0

私のコードはうまくいきました。ジオフェンシングがネットワーク/無線LAN情報を使用してGeofenceトランジションをトリガするため、エミュレータのジオフェンシングでは機能しません。物理デバイスでテストしたところ、私のコードはうまくいった。

関連する問題