Esri's Geotrigger Service is replacing Geoloqi! Learn More | FAQ

Handling Events - Android SDK

If you want to be notified when certain events occur, such as the logging of a new location fix or the triggering of a nearby geofence, you should extend the abstract class LQBroadcastReceiver, which is an implementation of BroadcastReceiver. There are several ways to do this, the quickest is to dynamically register the receiver as an anonymous inner class in one of your application's Activity classes.

@Override
public void onResume() {
    super.onResume();

    // Define what BroadcastIntent actions your receiver will
    // listen for. In this case we are registering to receive only
    // broadcast intents with the ACTION_LOCATION_CHANGED action.
    final IntentFilter filter = new IntentFilter();
    filter.addAction(LQBroadcastReceiver.ACTION_LOCATION_CHANGED);

    // Register our broadcast receiver with the IntentFilter
    // created above.
    registerReceiver(new LQBroadcastReceiver() {
        @Override
        public void onLocationChanged(Context context, Location location) {
            // Let your imagination run wild here...
        }
    }, filter);
}

Note that you may want to unregister the receiver in your activity's onPause method. In that case it may be easier to bind the receiver to a private property on the Activity.

private LQBroadcastReceiver mLocationReceiver = new LQBroadcastReceiver() {
    @Override
    public void onLocationChanged(Context context, Location location) {
        // Let your imagination run wild here...
    }
};        

@Override
public void onResume() {
    super.onResume();

    // Register our broadcast receiver
    registerReceiver(mLocationReceiver, filter);
}

@Override
public void onPause() {
    super.onPause();

    // Unregister our broadcast receiver
    unregisterReceiver(mLocationReceiver);
}

In either case, your Activity will be notified only when it has focus and is in the foreground. If you would like to intercept broadcast intents from the location tracker at any point, even when your application is not in the foreground, you should register the broadcast receiver from your application's manifest file.

<application>
    <receiver
        android:name=".receiver.SampleReceiver"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="com.geoloqi.android.sdk.action.LOCATION_CHANGED" />
        </intent-filter>
    </receiver>
</application>

In this case you provide an implementation of LQBroadcastReceiver named SampleReceiver in the receiver package. The actions you would like your receiver to be sent are listed in the ... section just like the previous example. This will ensure your broadcast receiver will always be notified, even if your application is not currently running. This allows you a great deal of flexibility and provides you with the opportunity to do everything from creating a Notification to launching a new Activity. If you would like to create a dual-purpose broadcast receiver, this is a good pattern to follow.

@Override
public void onLocationChanged(Context context, Location location) {
    try {
        OnLocationChangedListener listener = (OnLocationChangedListener) context;
        listener.onLocationChanged(location);
    } catch (ClassCastException e) {
        // The broadcast receiver is running with a Context that
        // does not implement OnLocationChangedListener. If your activity
        // has implemented the interface, then this generally means
        // that the receiver is running in a global context and
        // is not bound to any particular activity.
    }
}

public interface OnLocationChangedListener {
    public void onLocationChanged(Location location);
}

From here you can register the receiver in your activities and implement any of the public interfaces you've defined on the receiver. The interface methods will then be called if the activity is in the foreground when the receiver gets a new broadcast.