地理围栏未触发

时间:2015-10-04 19:30:32

标签: android google-maps geofencing

我试图在我的APP上实施Geofence,问题是当我登上我标记的地方时不会触发。

我做的是设置Latitude&来自Place PickerLongitude

我得到的价值很好,因为我把它放在google maps上,而且这个地方是正确的,因为我已经阅读了许多人们设置错误值的答案来自lat / long

我有两个班级:GeofenceSingleton.classGeofenceTransitionsIntentService

GeofenceSingleton.class看起来像:

public class GeofenceSingleton implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> {

private GoogleApiClient googleApiClient;
private static GeofenceSingleton _singleInstance;
private static Context appContext;
private static final String ERR_MSG = "Application Context is not set!! " +
        "Please call GeofenceSngleton.init() with proper application context";
private PendingIntent mGeofencePendingIntent;
private static ArrayList<Geofence> mGeofenceList;


private GeofenceSingleton() {
    if (appContext == null)
        throw new IllegalStateException(ERR_MSG);

    this.googleApiClient = new GoogleApiClient.Builder(this.appContext)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    this.googleApiClient.connect();
    mGeofenceList = new ArrayList<Geofence>();
}

/**
 * @param applicationContext
 */
public static void init(Context applicationContext) {
    appContext = applicationContext;
}

public static GeofenceSingleton getInstance() {
    if (_singleInstance == null)
        synchronized (GeofenceSingleton.class) {
            if (_singleInstance == null)
                _singleInstance = new GeofenceSingleton();
        }
    return _singleInstance;
}

@Override
public void onConnected(Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

public void addGeofence(Location location, String uid) {
    mGeofenceList.add(new Geofence.Builder()
            .setRequestId(uid)
            .setCircularRegion(
                    location.getLatitude(),
                    location.getLongitude(),
                    500
            )
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
            .setExpirationDuration(1000000)
            .build());
}

private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofences(mGeofenceList);
    return builder.build();
}

private PendingIntent getGeofencePendingIntent() {
     // Reuse the PendingIntent if we already have it.
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }
    Intent intent = new Intent(appContext, GeofenceSingleton.class);
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
    // addGeofences() and removeGeofences().
    return PendingIntent.getService(appContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

public void startGeofencing() {
    if (!googleApiClient.isConnected()) {
        Toast.makeText(appContext, "API client not connected", Toast.LENGTH_SHORT).show();
        return;
    }

    LocationServices.GeofencingApi.addGeofences(
            googleApiClient,
     // The GeofenceRequest object.
            getGeofencingRequest(),
    // A pending intent that that is reused when calling removeGeofences(). This
    // pending intent is used to generate an intent when a matched geofence
    // transition is observed.
            getGeofencePendingIntent()
    ).setResultCallback(this); // Result processed in onResult().
    Toast.makeText(appContext,"Geofencing started", Toast.LENGTH_LONG).show();
}



public void removeGeofence(){
    LocationServices.GeofencingApi.removeGeofences(
            googleApiClient,
// This is the same pending intent that was used in addGeofences().
            getGeofencePendingIntent()
    );
}

@Override
public void onResult(Status status) {
    if (status.isSuccess()) {
// Update state and save in shared preferences.

        Toast.makeText(
                appContext,
                "YO",
                Toast.LENGTH_SHORT
        ).show();
    } else {
        Toast.makeText(
                appContext,
                "NOO",
                Toast.LENGTH_SHORT
        ).show();
    }
}
}

GeofenceTransitionsIntentService

public class GeofenceTransitionsIntentService extends IntentService {

private static final String TAG = "Geofence-Service";
private Handler handler;
SharedPreferences sp;
SharedPreferences.Editor editor;
String EmailName, MessageEmail;
Context mContext;
Boolean EmailSent = false;

public GeofenceTransitionsIntentService() {
    super(TAG);
}

@Override
public void onCreate() {
    super.onCreate();
    this.mContext = this;
    sp = PreferenceManager.getDefaultSharedPreferences(this);
    handler = new Handler();
}

@Override
protected void onHandleIntent(Intent intent) {

    final GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        Log.e(TAG, "Error in geofencing event");
        return;
    }

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

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

        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "Entered", Toast.LENGTH_SHORT).show();
                sendNotification();
            }
        });
        Log.i(TAG, "Entered");


    }

    else {
        // Log the error.
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "Sorry you are out", Toast.LENGTH_SHORT).show();
            }
        });
        Log.e(TAG, String.valueOf(geofenceTransition));
    }

}

private void sendNotification() {
    Toast.makeText(GeofenceTransitionsIntentService.this, "HEEEEEEEY I'M IN", Toast.LENGTH_SHORT).show();



}

在我的MainActivity我得到了这个:

GeofenceSingleton.init(this); //If I don't call this the class doesn't work
this.geofenceSingleton = GeofenceSingleton.getInstance();

然后我按如下方式构建Geofence

Location geofence = new Location("");
geofence.setLatitude(Double.parseDouble(latitude));
geofence.setLongitude(Double.parseDouble(longitude));
geofenceSingleton.addGeofence(geofence, GeofenceKey);
geofenceSingleton.startGeofencing();

注意

我在<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

上添加了manifest.xml

我将service声明为<service android:name=".GeofenceTransitionsIntentService"/>

我在build.gradlecompile 'com.google.android.gms:play-services:7.8.0'

上添加了这一行

它只显示Toast.makeText(appContext,"YO",Toast.LENGTH_SHORT).show();因为状态成功,并且还显示Toast.makeText(appContext,"Geofencing started", Toast.LENGTH_LONG).show();表示它已启动。

问题

为什么我的IntentService永远不会被调用?

我测试了它放置了我家的Lat / Long,因为我应该触发它,不是吗?

4 个答案:

答案 0 :(得分:6)

它永远不会起作用,因为你正在做的是

Intent intent = new Intent(appContext, GeofenceSingleton.class);

将意图传递给GeofenceSingleton?

您需要做的是

Intent intent = new Intent(appContext, GeofenceTransitionsIntentService.class);

答案 1 :(得分:0)

如果您的意图已经使用正确的类构造(请参见varunkr's answer),则还需要确保服务也包含在AndroidManifest.xml中。

<application
    ...

    <service android:name=".GeofencingTransitionIntentService" />

答案 2 :(得分:0)

确保您的服务和广播接收在清单中启用了这些属性。

  android:enabled="true"
  android:exported="true"

答案 3 :(得分:-3)

由于我已回答hereGeofence API类似于被动位置接收器,即当您需要LocationServices.FusedLocationApi.requestLocationUpdates工作时,您必须并行请求来自Geofence的坐标。< / p>