发生地理围栏转换时,我的GeofenceTransitionsIntentService不会发送任何通知

时间:2015-06-18 16:34:07

标签: android android-geofence

我正在尝试实现添加和监控地理围栏的功能,我认为this tutorial是最好的开始。

我已经完成了所有类似于他们所做的事情,并且它有效但不像我想要的那样。我注意到没有跟踪地理围栏过渡,我实际想要说的是当我进入地理围栏时,没有任何反应,但是当我在地理围栏中启动应用程序时,我得到了一些回应。我还注意到我的GeofenceTransitionsIntentService中没有任何方法被执行,这实际上意味着地理围栏转换细节永远不会被广播到MainActivity。

老实说,这是我第一次尝试实现这种功能,所以我真的不知道这个问题的原因是什么。如果您有任何想法,请回复,我将不胜感激。

这是我到目前为止所做的事情

我的AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="at.at.tuwien.hci.hciss2015">

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <!-- activities -->
    <service android:name="at.at.tuwien.hci.hciss2015.util.GeofenceTransitionsIntentService" />
</application>

My GeofenceTransitionsIntentService.java

public class GeofenceTransitionsIntentService extends IntentService {

protected static final String TAG = GeofenceTransitionsIntentService.class.getSimpleName();

public GeofenceTransitionsIntentService() {
    super(TAG);
}

@Override
public void onCreate() {
    super.onCreate();
    Log.e(TAG, "init GeofenceTransitionsIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        Log.e(TAG, "geofencing event error");
        return;
    }

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

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

        // Get the geofences that were triggered. A single event can trigger multiple geofences.
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

        // Get the transition details as a String.
        String geofenceTransitionDetails = getGeofenceTransitionDetails(
            this,
            geofenceTransition,
            triggeringGeofences
        );
        Log.e(TAG, geofenceTransitionDetails);
        // Send notification and log the transition details.
        sendNotification(geofenceTransitionDetails);
        Log.i(TAG, geofenceTransitionDetails);
    } else {
        Log.e(TAG, "geofence transition invalid type");
    }
}

private String getGeofenceTransitionDetails(
        Context context,
        int geofenceTransition,
        List<Geofence> triggeringGeofences) {

    String geofenceTransitionString = getTransitionString(geofenceTransition);

    // Get the Ids of each geofence that was triggered.
    ArrayList triggeringGeofencesIdsList = new ArrayList();
    for (Geofence geofence : triggeringGeofences) {
        triggeringGeofencesIdsList.add(geofence.getRequestId());
    }
    String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList);

    return geofenceTransitionString + ": " + triggeringGeofencesIdsString;
}

private void sendNotification(String notificationDetails) {
    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

    // Construct a task stack.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // Add the main Activity to the task stack as the parent.
    stackBuilder.addParentStack(MainActivity.class);

    // Push the content Intent onto the stack.
    stackBuilder.addNextIntent(notificationIntent);

    // Get a PendingIntent containing the entire back stack.
    PendingIntent notificationPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    // Get a notification builder that's compatible with platform versions >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Define the notification settings.
    builder.setContentTitle(notificationDetails)
            .setContentText("Click notification to return to app")
            .setContentIntent(notificationPendingIntent);

    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}

private String getTransitionString(int transitionType) {
    switch (transitionType) {
        case Geofence.GEOFENCE_TRANSITION_ENTER:
            return "transition - enter";
        case Geofence.GEOFENCE_TRANSITION_EXIT:
            return "transition - exit";
        default:
            return "unknown transition";
    }
}
}

我的主要活动

public class MainActivity extends FragmentActivity implements
ConnectionCallbacks, OnConnectionFailedListener, ResultCallback<Status> {

protected GoogleApiClient mGoogleApiClient;

protected ArrayList<Geofence> mGeofenceList;

private PendingIntent mGeofencePendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
....

    mGeofenceList = new ArrayList<Geofence>();

    mGeofencePendingIntent = null;

    populateGeofenceList();

    buildGoogleApiClient();
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .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() {
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }
    Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

public void populateGeofenceList() {
    mGeofenceList.add(new Geofence.Builder()
            .setRequestId("1")
            .setCircularRegion(48.178454, 16.369699, 10)
            .setExpirationDuration(Geofence.NEVER_EXPIRE)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
            .build());

    mGeofenceList.add(new Geofence.Builder()
            .setRequestId("2")
            .setCircularRegion(48.17755, 16.369114, 10)
            .setExpirationDuration(Geofence.NEVER_EXPIRE)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
            .build());
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();

    LocationServices.GeofencingApi.removeGeofences(
        mGoogleApiClient,
        getGeofencePendingIntent()
    ).setResultCallback(this);

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

@Override
public void onConnected(Bundle bundle) {
    Location myLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    initCamera(myLocation);

    LocationServices.GeofencingApi.addGeofences(
        mGoogleApiClient,
        getGeofencingRequest(),
        getGeofencePendingIntent()
    ).setResultCallback(this);
}

@Override
public void onConnectionSuspended(int cause) {
    Log.i(TAG, "Connection suspended");
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}

@Override
public void onResult(Status status) {
    if (status.isSuccess()) {

        Toast.makeText(this, "status: " + status.getStatus(),Toast.LENGTH_SHORT).show();
    } else {
        Log.e(TAG, "something is wrong");
    }
}

}

0 个答案:

没有答案