Intent not received from Android Geofence event
Asked Answered
A

1

11

Everything builds correctly and runs in the emulator, but I cannot seem to get my IntentService to log anything. I'm sure there's something basic that I'm missing or overlooking, but I'm pretty new to Android/Java and have run out of ideas at this point.

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private Geofence geofence;
private PendingIntent mGeofencePendingIntent;
private GoogleApiClient mGoogleApiClient;

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

    LatLng latLng = new LatLng(39.7492350, -104.9913250);

    geofence = new Geofence.Builder()
            .setRequestId(GEOFENCE_REQ_ID)
            .setCircularRegion(latLng.latitude, latLng.longitude, GEOFENCE_RADIUS_IN_METERS)
            .setExpirationDuration(GEOFENCE_EXPIRATION)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
}

private boolean checkPermission() {
    Log.i(TAG, "MainActivity.checkPermission()");
    return (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
}

private GeofencingRequest getGeofencingRequest() {
    Log.i(TAG, "MainActivity.getGeofencingRequest()");
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofence(geofence);
    return builder.build();
}

private PendingIntent getGeofencePendingIntent() {
    Log.i(TAG, "MainActivity.getGeofencePendingIntent()");
    if (null != mGeofencePendingIntent) {
        return mGeofencePendingIntent;
    }

    Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

@Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "MainActivity.onConnected()");
    if (checkPermission()) {
        mGeofencePendingIntent = getGeofencePendingIntent();
        LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, getGeofencingRequest(), mGeofencePendingIntent);
    } else {
        Log.i(TAG, "Permission not granted");
    }
}

@Override
public void onConnectionSuspended(int i) {
    Log.i(TAG, "MainActivity.onConnectionSuspended()");
    if (null != mGeofencePendingIntent) {
        LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, mGeofencePendingIntent);
    }
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i(TAG, "MainActivity.onConnectionFailed()");
}}

Whenever I update that device lat/lng using the emulator or the android console, no intent is received by the following service:

public class GeofenceTransitionsIntentService extends IntentService {

public GeofenceTransitionsIntentService() {
    super(GeofenceTransitionsIntentService.class.getSimpleName());
}

@Override
protected void onHandleIntent(Intent intent) {
    Log.i(TAG, "GeofenceTransitionsIntentService.onHandleIntent()");

    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        int errorCode = geofencingEvent.getErrorCode();
        Log.e(TAG, "Location Services error: " + errorCode);
    } else {
        Log.i(TAG, "geofencingEvent was successful");
    }
}}

Manifest:

<?xml version="1.0" encoding="utf-8"?>

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".GeofenceTransitionsIntentService" />
</application>

I've been referencing the following resources: Android Documentation and Google Sample.

Afoul answered 11/8, 2016 at 16:40 Comment(2)
I few things you didn't clarify: 1-Have you tested it on a real device? 2-Are you inside the specified lat/lng? 3-Is Google Play Services installed? 4-Have you tried opening Google Maps and then updating the lat/lng? Sometimes this is the only way to get GPS working on an emulator.Laniary
Please run your app on a real device and then tell.Baudelaire
A
1

Location services do not appear to be fully functional in the emulator. Once I plugged in a real device everything worked as expected.

Afoul answered 15/8, 2016 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.