NEON API Developer Tutorial

Sarah Blaszak -

In this example we will construct a simple Android Activity that listens for NEON location updates and then prints those updates to the console. Note that the NEON API provides helpful toString() methods on data objects to aid in development.

  1. We will start by creating a new Activity that implements the INeonLocationListener interface. This interface can be implemented by an activity or any other class in order to receive location updates.

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import com.trx.neon.api.Neon;
    import com.trx.neon.api.INeonLocationListener;
    import com.trx.neon.model.DeviceLocation;
    
    public class MainActivity 
        extends Activity 
        implements INeonLocationListener
    {
        @Override
        public void onLocationChanged(DeviceLocation location) 
        {
            Log.i("MyFirstLocation", location.toString());
        }
    }
    
  2. Next we need to register the INeonLocationListener instance with the NEON API. The NEON API contains the classNeon with several static methods which provide a simple way to configure the NEON API. In the example below we override the onCreate method and register our listener with NEON for location updates.

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        Neon.registerLocationUpdates(this);
    }
    
  3. Internally, the NEON API utilizes Android Binders to handle communication between the end user application and the NEON Location Service. When using the NEON API, the developer is responsible for binding and unbinding the NEON Location Service. Note that the NEON Location Service will not be running unless it is bound and must remain bound throughout the time that location updates are desired. In the example below we bind the NEON Location Service in theonStart method and unbind in the onStop method.

    @Override
    protected void onStart()
    {
        super.onStart();
        Neon.bind(this.getApplicationContext());
    }
    
    @Override
    protected void onStop()
    {
        super.onStop();
        Neon.unbind();
    }
  4. Below is the full source code for the Android Activity that you just created.  
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.trx.neon.api.Neon;
import com.trx.neon.api.INeonLocationListener;
import com.trx.neon.model.DeviceLocation;

public class MainActivity 
    extends Activity 
    implements INeonLocationListener
{
    @Override
    public void onLocationChanged(DeviceLocation location) 
    {
        Log.i("MyFirstLocation", location.toString());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        Neon.registerLocationUpdates(this);
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        Neon.bind(this.getApplicationContext());
    }

    @Override
    protected void onStop()
    {
        super.onStop();
        Neon.unbind();
    }
}
Have more questions? Submit a request

Comments

Powered by Zendesk