A listener is easy enough. You need an interface (I'll continue the form of the example code in the wiki):
Code:
package com.mycompany.locationservices;
public interface LocationListener {
// use separate arguments, because we can't pass a Location object to the main app.
public void newLocation(double lat, double long, float altitude, float horizAcc, float vertAcc);
}
Modify the abstract class:
Code:
public abstract class LocationProvider {
// this class doesn't import the Location API, so this is our own listener
private LocationListener listener;
public void setLocationListener(LocationListener l) {
listener = l;
}
protected void notifyListener(double lat, double long, float altitude, float horizAcc, float vertAcc) {
if (listener != null) {
listener.newLocation(lat, long, altitude, horizAcc, vertAcc);
}
}
}
In the implementation class, implement the Location API's LocationListener, and when you receive an event, get the location information and pass it to notifyListener(), which will then forward the details to the rest of the application for you.
We have to create a new listener interface, because we can't use the Location API's interface outside of the location implementation package. If we did use the Location API's interface in the main application code, we'd cause the device to look for the Location API, and fail on devices that don't have it (same for any other API).
Similarly, which the Location API's interface specifies a QualifiedLocation object as the argument to the listener method, we have to use separate arguments (or create our own "location" class). Again, if the main application code contains a reference to a Location API interface or class, it may fail on devices with no Location API.
You will need the Location API (or NFC, or whatever) in the class path when building, or the classes that refer to those APIs won't compile or preverify. This won't cause a problem when running on devices without that API, provided you stick to the rules I've described.
Graham.