Bind to API

In order to use the NEON API, we first need to connect using Neon.bind(). Let’s create a function that connects and disconnects from the NEON API:


public static final int PERMISSION_ACTIVITY_REQUEST_CODE = 1003;

void startLocationService() 
{

    if(!Neon.hasRequiredPermissions(getApplicationContext()))
    {
        Log.i(LOG_TAG, "starting NEON permissions activity");
        Neon.startPermissionActivityForResult(this, PERMISSION_ACTIVITY_REQUEST_CODE);
    }
    else
    {
        if (!Neon.isBound())
        {
            Log.i(LOG_TAG, "starting NEON Location Service");
            Neon.bind(getApplicationContext());
        }
    }
}

void stopLocationService() 
{
    if (Neon.isBound())
    {
        Log.i(LOG_TAG, "shutting down NEON Location Service");
        Neon.unbind();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    switch (requestCode) 
    {
        case PERMISSION_ACTIVITY_REQUEST_CODE:
            switch (resultCode) 
            {
                case Activity.RESULT_CANCELED:
                    Log.i(LOG_TAG, "no permission granted, exit");
                    finish();
                case Activity.RESULT_OK:
                    Log.i(LOG_TAG, "permissions granted, start the location service");
                    startLocationService();
                    break;
                default:
                    break;
            }
            break;
    }
}

@Override
protected void onResume()
{
    super.onResume();
    startLocationService();
}

@Override
protected void onPause()
{
    super.onPause();
    if(isFinishing())
        shutdown();
}

private void shutdown()
{
    stopLocationService();
}

This code does several things. When the Activity is started, startLocationService() will check whether the NEON Location Service has its required permissions. If not, it will call Neon.startPermissionActivityForResult(), which will prompt the user to grant the permissions. An activity result code will be returned. If the result is RESULT_CANCELED, then the user has refused permissions and NEON cannot be started. If the result is RESULT_OK, then permissions have been granted and NEON should then be started.

In order to prevent the NEON API from being unbound when the orientation changes, you can add android:configChanges="orientation|screenSize" to your android manifest so configuration changes do not shutdown and restart the activity:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.trxsystems.neon.neonsampleapp" >
    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps"
            android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>