我正在尝试测试我的android geofencing应用。我已输入当前位置作为地理围栏纬度,位置并尝试测试应用程序是否有效。从技术上讲,当我运行应用程序时,我在地理围栏区域内。但是,当我启动应用程序时,我没有收到任何通知 - 当我在地理围栏区域内时,收到的转换类型为-1。我需要加入其他东西吗?以下是我目前的代码 -
//once connected to GoogleAPIClient
ArrayList<Geofence> geofences = new ArrayList<Geofence>();
geofences.add(new Geofence.Builder()
.setRequestId("id")
.setCircularRegion(
lat, lon, GEOFENCERADIUS)
.setTransitionTypes(
Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_DWELL
| Geofence.GEOFENCE_TRANSITION_EXIT)
.setLoiteringDelay(30000) // check every 30 seconds
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build());
System.out.println("GEOFENCE ADDED");
}
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(geofences);
LocationRequest locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setFastestInterval(5000L).setInterval(10000L)
.setSmallestDisplacement(75.0F);
PendingIntent pendingIntent = PendingIntent.getService(this, 0,
new Intent(this, GeofenceReceiver.class),
PendingIntent.FLAG_UPDATE_CURRENT);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
locationRequest, pendingIntent);
意向服务类如下:
public class GeofenceReceiver extends IntentService {
public GeofenceReceiver()
{
super("GeoFenceReceiver");
}
@Override
protected void onHandleIntent(Intent intent) {
System.out.println("INTENT RECEIVED");
GeofencingEvent geoEvent=GeofencingEvent.fromIntent(intent);
if (geoEvent.hasError()) {
//todo error process
System.out.println("Error");
}
else {
int transitionType = geoEvent.getGeofenceTransition();
System.out.println(transitionType);
if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ||
transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) {
List<Geofence> triggerList = geoEvent.getTriggeringGeofences();
for (Geofence geofence : triggerList) {
generateNotification(geofence.getRequestId(), "address you defined");
}
}
if(transitionType==-1)
{
// System.out.println(intent.getAction());
}
}
当我运行应用程序时,我收到消息INTENT RECEIVED,但是,转换类型始终为-1。这是应用程序的工作问题吗?
PS:我确信这与GPS准确性无关,因为我已将地理围栏半径增加到2公里。所以我确信它有一些表示运行应用程序的过渡。