{"_id":"571be732395f6e0e007437d1","githubsync":"","parentDoc":null,"version":{"_id":"56a83b989ec7660d002e07c1","project":"56a83b979ec7660d002e07be","__v":9,"createdAt":"2016-01-27T03:38:00.333Z","releaseDate":"2016-01-27T03:38:00.333Z","categories":["56a83b989ec7660d002e07c2","56a83c282036420d002d21e1","56a96de8f834950d0037b35a","56a9706013a69a0d00a778c3","56a970ec2d8fd90d0036eec7","56a971a62bb3910d000ee934","56a973372d8fd90d0036eece","56a978dc2bb3910d000ee93f","571d5ae118b3c10e003e55cd"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"Beta","version_clean":"1.0.0","version":"1.0"},"__v":12,"user":"56a975c12d8fd90d0036eed0","category":{"_id":"571d5ae118b3c10e003e55cd","version":"56a83b989ec7660d002e07c1","__v":0,"project":"56a83b979ec7660d002e07be","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-04-24T23:46:41.975Z","from_sync":false,"order":0,"slug":"tutorials","title":"Tutorials"},"project":"56a83b979ec7660d002e07be","updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-04-23T21:20:50.257Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"<div class=\"corner-ribbon top-left sticky blue\"><a style=\"color: white;\" href=\"https://pathfinder.readme.io/blog/pathfinder-now-in-public-beta\">Public Beta!</a></div>\n\nThis 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](doc:getting-started).\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Introduction\"\n}\n[/block]\nIn 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.\n\n- Authentication\n- Clusters\n- Commodities\n- Transports\n\nThe 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](doc:android).\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Listener basics\"\n}\n[/block]\nEach 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.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// MyClusterListener extends ClusterListener\\nCluster cluster = pathfinder.getDefaultCluster();\\n\\nClusterListener listener = new MyClusterListener();\\n\\n// Add the listener\\ncluster.addListener(listener);\\n\\n// Remove the listener\\ncluster.removeListener(listener);\",\n      \"language\": \"java\",\n      \"name\": \"Android/Java\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Authentication Listener\"\n}\n[/block]\nEach 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.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"AuthenticationListener listener = new MyAuthenticationListener();\\n\\n// Add an authentication listener\\npathfinder.addAuthenticationListener(listener);\\n\\n// Remove an authentication listener\\npathfinder.removeAuthenticationListener(listener);\",\n      \"language\": \"java\",\n      \"name\": \"Android/Java\"\n    }\n  ]\n}\n[/block]\nHere is an example authentication listener implementation.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import xyz.thepathfinder.android.AuthenticationListener;\\n\\npublic class MyAuthenticationListener extends AuthenticationListener {\\n    :::at:::Override\\n    public void authenticationSuccessful() {\\n        // Do something\\n    }\\n\\n    @Override\\n    public void authenticationFailed(String reason) {\\n        // Do something\\n    }\\n}\",\n      \"language\": \"java\",\n      \"name\": \"Android/Java\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Cluster Listener\"\n}\n[/block]\nEach 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.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import xyz.thepathfinder.android.Cluster;\\nimport xyz.thepathfinder.android.ClusterListener;\\nimport xyz.thepathfinder.android.Route;\\n\\nimport java.util.List;\\n\\npublic class MyClusterListener extends ClusterListener{\\n    @Override\\n    public void routed(List<Route> routes) {\\n        \\n    }\\n\\n    @Override\\n    public void connected(Cluster model) {\\n    \\n    }\\n\\n    @Override\\n    public void created(Cluster model) {\\n    \\n    }\\n\\n    @Override\\n    public void deleted(Cluster model) {\\n    \\n    }\\n\\n    @Override\\n    public void error(String error) {\\n    \\n    }\\n\\n    @Override\\n    public void updated(Cluster model) {\\n    \\n    }\\n\\n    @Override\\n    public void subscribed(Cluster model) {\\n    \\n    }\\n}\",\n      \"language\": \"java\",\n      \"name\": \"Android/Java\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Commodity Listener\"\n}\n[/block]\nEach 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.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import xyz.thepathfinder.android.Commodity;\\nimport xyz.thepathfinder.android.CommodityListener;\\nimport xyz.thepathfinder.android.Route;\\nimport xyz.thepathfinder.android.Transport;\\n\\npublic class MyCommodityListener extends CommodityListener {\\n    @Override\\n    public void routed(Route route) {\\n        \\n    }\\n\\n    @Override\\n    public void transportUpdated(Transport transport) {\\n    \\n    }\\n\\n    @Override\\n    public void connected(Commodity model) {\\n    \\n    }\\n\\n    @Override\\n    public void created(Commodity model) {\\n    \\n    }\\n\\n    @Override\\n    public void updated(Commodity model) {\\n    \\n    }\\n\\n    @Override\\n    public void subscribed(Commodity model) {\\n    \\n    }\\n\\n    @Override\\n    public void startLocationUpdated(double latitude, double longitude) {\\n    \\n    }\\n\\n    @Override\\n    public void endLocationUpdated(double latitude, double longitude) {\\n    \\n    }\\n}\",\n      \"language\": \"java\",\n      \"name\": \"Android/Java\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Transport Listener\"\n}\n[/block]\nEach 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.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import xyz.thepathfinder.android.Commodity;\\nimport xyz.thepathfinder.android.Route;\\nimport xyz.thepathfinder.android.Transport;\\nimport xyz.thepathfinder.android.TransportListener;\\n\\nimport java.util.List;\\n\\npublic class MyTransportListener extends TransportListener {\\n    @Override\\n    public void routed(Route route) {\\n    \\n    }\\n\\n    @Override\\n    public void locationUpdated(double latitude, double longitude) {\\n    \\n    }\\n\\n    @Override\\n    public void connected(Transport model) {\\n    \\n    }\\n\\n    @Override\\n    public void created(Transport model) {\\n    \\n    }\\n\\n    @Override\\n    public void updated(Transport model) {\\n    \\n    }\\n\\n    @Override\\n    public void commoditiesUpdated(List<Commodity> commodities) {\\n    \\n    }\\n}\\n\",\n      \"language\": \"java\",\n      \"name\": \"Android/Java\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"androidjava-listeners","type":"basic","title":"Android/Java"}
<div class="corner-ribbon top-left sticky blue"><a style="color: white;" href="https://pathfinder.readme.io/blog/pathfinder-now-in-public-beta">Public Beta!</a></div> 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](doc:getting-started). [block:api-header] { "type": "basic", "title": "Introduction" } [/block] 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](doc:android). [block:api-header] { "type": "basic", "title": "Listener basics" } [/block] 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. [block:code] { "codes": [ { "code": "// MyClusterListener extends ClusterListener\nCluster cluster = pathfinder.getDefaultCluster();\n\nClusterListener listener = new MyClusterListener();\n\n// Add the listener\ncluster.addListener(listener);\n\n// Remove the listener\ncluster.removeListener(listener);", "language": "java", "name": "Android/Java" } ] } [/block] [block:api-header] { "type": "basic", "title": "Authentication Listener" } [/block] 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. [block:code] { "codes": [ { "code": "AuthenticationListener listener = new MyAuthenticationListener();\n\n// Add an authentication listener\npathfinder.addAuthenticationListener(listener);\n\n// Remove an authentication listener\npathfinder.removeAuthenticationListener(listener);", "language": "java", "name": "Android/Java" } ] } [/block] Here is an example authentication listener implementation. [block:code] { "codes": [ { "code": "import xyz.thepathfinder.android.AuthenticationListener;\n\npublic class MyAuthenticationListener extends AuthenticationListener {\n @Override\n public void authenticationSuccessful() {\n // Do something\n }\n\n @Override\n public void authenticationFailed(String reason) {\n // Do something\n }\n}", "language": "java", "name": "Android/Java" } ] } [/block] [block:api-header] { "type": "basic", "title": "Cluster Listener" } [/block] 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. [block:code] { "codes": [ { "code": "import xyz.thepathfinder.android.Cluster;\nimport xyz.thepathfinder.android.ClusterListener;\nimport xyz.thepathfinder.android.Route;\n\nimport java.util.List;\n\npublic class MyClusterListener extends ClusterListener{\n @Override\n public void routed(List<Route> routes) {\n \n }\n\n @Override\n public void connected(Cluster model) {\n \n }\n\n @Override\n public void created(Cluster model) {\n \n }\n\n @Override\n public void deleted(Cluster model) {\n \n }\n\n @Override\n public void error(String error) {\n \n }\n\n @Override\n public void updated(Cluster model) {\n \n }\n\n @Override\n public void subscribed(Cluster model) {\n \n }\n}", "language": "java", "name": "Android/Java" } ] } [/block] [block:api-header] { "type": "basic", "title": "Commodity Listener" } [/block] 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. [block:code] { "codes": [ { "code": "import xyz.thepathfinder.android.Commodity;\nimport xyz.thepathfinder.android.CommodityListener;\nimport xyz.thepathfinder.android.Route;\nimport xyz.thepathfinder.android.Transport;\n\npublic class MyCommodityListener extends CommodityListener {\n @Override\n public void routed(Route route) {\n \n }\n\n @Override\n public void transportUpdated(Transport transport) {\n \n }\n\n @Override\n public void connected(Commodity model) {\n \n }\n\n @Override\n public void created(Commodity model) {\n \n }\n\n @Override\n public void updated(Commodity model) {\n \n }\n\n @Override\n public void subscribed(Commodity model) {\n \n }\n\n @Override\n public void startLocationUpdated(double latitude, double longitude) {\n \n }\n\n @Override\n public void endLocationUpdated(double latitude, double longitude) {\n \n }\n}", "language": "java", "name": "Android/Java" } ] } [/block] [block:api-header] { "type": "basic", "title": "Transport Listener" } [/block] 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. [block:code] { "codes": [ { "code": "import xyz.thepathfinder.android.Commodity;\nimport xyz.thepathfinder.android.Route;\nimport xyz.thepathfinder.android.Transport;\nimport xyz.thepathfinder.android.TransportListener;\n\nimport java.util.List;\n\npublic class MyTransportListener extends TransportListener {\n @Override\n public void routed(Route route) {\n \n }\n\n @Override\n public void locationUpdated(double latitude, double longitude) {\n \n }\n\n @Override\n public void connected(Transport model) {\n \n }\n\n @Override\n public void created(Transport model) {\n \n }\n\n @Override\n public void updated(Transport model) {\n \n }\n\n @Override\n public void commoditiesUpdated(List<Commodity> commodities) {\n \n }\n}\n", "language": "java", "name": "Android/Java" } ] } [/block]