Android/Java
This tutorial will walk you through how to use the listeners in the Android/Java SDK. It is expected that you have already read Getting Started.
Introduction
In the Android/Java SDK there are four listeners available to be extended. Each listener is an abstract class with all the methods not declared as abstract. This allows you to implement only the methods you care about without cluttering your implementation. To get the most out of each listener you must add it to the object right after it is locally created by the SDK. If you add the listener after calling any connect, create, delete, or update method the Pathfinder server response may be missed. Although, the SDK will appropriately update the object when the Pathfinder server sends it a message, even if listeners are not being used. The four listeners are for the following areas.
- Authentication
- Clusters
- Commodities
- Transports
The authentication listener helps you determine if the user successfully authenticated with the Pathfinder service. The cluster, commodity, and transport listeners tell you when anything in the object changes. Note, you may add as many listeners for each object as you want. For more information about the listeners visit the Android/Java SDK Documentation.
Listener basics
Each type of listener allows you to add and remove listeners of specific types. For example, to add an authentication listener it must extend the AuthenticationListener class.
// MyClusterListener extends ClusterListener
Cluster cluster = pathfinder.getDefaultCluster();
ClusterListener listener = new MyClusterListener();
// Add the listener
cluster.addListener(listener);
// Remove the listener
cluster.removeListener(listener);
Authentication Listener
Each authentication listener must extend the AuthenticationListener class and can be added to Pathfinder objects. It should be added before the connect method is called, so you know if the authentication sequence succeed or failed. Also, note that adding and removing an authentication listener is a little different than adding or removing any other listener.
AuthenticationListener listener = new MyAuthenticationListener();
// Add an authentication listener
pathfinder.addAuthenticationListener(listener);
// Remove an authentication listener
pathfinder.removeAuthenticationListener(listener);
Here is an example authentication listener implementation.
import xyz.thepathfinder.android.AuthenticationListener;
public class MyAuthenticationListener extends AuthenticationListener {
@Override
public void authenticationSuccessful() {
// Do something
}
@Override
public void authenticationFailed(String reason) {
// Do something
}
}
Cluster Listener
Each cluster listener must extend the ClusterListener class and can be added to cluster objects. If you don't want to miss any possible listener messages add the listener before calling any connect, create, delete, or update method. Note, you do not need to implement all the methods provided in the ClusterListener class. In the example below there are even more methods you may override, this is just a useful subset.
import xyz.thepathfinder.android.Cluster;
import xyz.thepathfinder.android.ClusterListener;
import xyz.thepathfinder.android.Route;
import java.util.List;
public class MyClusterListener extends ClusterListener{
@Override
public void routed(List<Route> routes) {
}
@Override
public void connected(Cluster model) {
}
@Override
public void created(Cluster model) {
}
@Override
public void deleted(Cluster model) {
}
@Override
public void error(String error) {
}
@Override
public void updated(Cluster model) {
}
@Override
public void subscribed(Cluster model) {
}
}
Commodity Listener
Each commodity listener must extend the CommodityListener class and can be added to commodity objects. If you don't want to miss any possible listener messages add the listener before calling any connect, create, delete, or update method. Note, you do not need to implement all the methods provided in the CommodityListener class. In the example below there are even more methods you may override, this is just a useful subset.
import xyz.thepathfinder.android.Commodity;
import xyz.thepathfinder.android.CommodityListener;
import xyz.thepathfinder.android.Route;
import xyz.thepathfinder.android.Transport;
public class MyCommodityListener extends CommodityListener {
@Override
public void routed(Route route) {
}
@Override
public void transportUpdated(Transport transport) {
}
@Override
public void connected(Commodity model) {
}
@Override
public void created(Commodity model) {
}
@Override
public void updated(Commodity model) {
}
@Override
public void subscribed(Commodity model) {
}
@Override
public void startLocationUpdated(double latitude, double longitude) {
}
@Override
public void endLocationUpdated(double latitude, double longitude) {
}
}
Transport Listener
Each transport listener must extend the TransportListener class and can be added to transport objects. If you don't want to miss any possible listener messages add the listener before calling any connect, create, delete, or update method. Note, you do not need to implement all the methods provided in the TransportListener class. In the example below there are even more methods you may override, this is just a useful subset.
import xyz.thepathfinder.android.Commodity;
import xyz.thepathfinder.android.Route;
import xyz.thepathfinder.android.Transport;
import xyz.thepathfinder.android.TransportListener;
import java.util.List;
public class MyTransportListener extends TransportListener {
@Override
public void routed(Route route) {
}
@Override
public void locationUpdated(double latitude, double longitude) {
}
@Override
public void connected(Transport model) {
}
@Override
public void created(Transport model) {
}
@Override
public void updated(Transport model) {
}
@Override
public void commoditiesUpdated(List<Commodity> commodities) {
}
}
Updated less than a minute ago