Integrating with Clover Android SDK

Integration with Clover Android SDK

Use the Clover Android SDK to access merchant data. For this example, we will display information on one inventory item.

📘

NOTE

For this tutorial, you must have one item in your merchant's inventory. You can do this through the Inventory app.

  1. To integrate clover-android-sdk into your project, begin by adding a dependency to your Gradle-enabled project for either the latest release or a specific SDK version number, if you prefer.
    Click on a tab below to see an example of each dependency.
dependencies {
  implementation 'com.clover.sdk:clover-android-sdk:latest.release'
}
dependencies {
  implementation 'com.clover.sdk:clover-android-sdk:VERSION_NUMBER'
}

Your gradle.build file should display the dependency declaration in the dependencies section. The following example shows the version number dependency on line 25.

  1. Add <uses-permission android:name="android.permission.GET_ACCOUNTS"/> to the AndroidManifest (this is required to connect with the merchant account).
  1. Add an ID to the textView element to enable dynamic text insertion.
  1. Finally, you can replace your Activity with this code to pull out the merchant data:
/*
1) Replace <your.package.name.here> below with your package name.
*/

package com.your.package.name.here;

import android.accounts.Account;
import android.os.AsyncTask;
import android.os.RemoteException;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.clover.sdk.util.CloverAccount;
import com.clover.sdk.v1.BindingException;
import com.clover.sdk.v1.ClientException;
import com.clover.sdk.v1.ServiceException;
import com.clover.sdk.v3.inventory.InventoryConnector;
import com.clover.sdk.v3.inventory.Item;

/*
2) To get or set any Clover data, go through Clover’s Connector classes, 
   such as InventoryConnector.
*/

public class MainActivity extends AppCompatActivity {

    private Account mAccount;
    private InventoryConnector mInventoryConnector;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

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

        mTextView = (TextView) findViewById(R.id.activity_main_text_view);

        // Retrieve the Clover account
        if (mAccount == null) {
            mAccount = CloverAccount.getAccount(this);

            if (mAccount == null) {
                return;
            }
        }

        // Connect InventoryConnector
        connect();

        // Get Item
        new InventoryAsyncTask().execute();
    }

 /*
3) Disconnect from the Clover Connectors when you aren’t using them to 
   follow Android best practices and let the connection end naturally.
*/
  
    @Override
    protected void onDestroy() {
        super.onDestroy();
        disconnect();
    }

    private void connect() {
        disconnect();
        if (mAccount != null) {
            mInventoryConnector = new InventoryConnector(this, mAccount, null);
            mInventoryConnector.connect();
        }
    }

    private void disconnect() {
        if (mInventoryConnector != null) {
            mInventoryConnector.disconnect();
            mInventoryConnector = null;
        }
    }

/*
4) Use async tasks when working with Clover Connector classes to minimize 
   work on the main UI thread while fetching or changing data. See 
   https://docs.clover.com/clover-platform/docs/clover-development-
   basics-android#section-async-tasks-and-executors.
*/  
  
    private class InventoryAsyncTask extends AsyncTask<Void, Void, Item> {

        @Override
        protected final Item doInBackground(Void... params) {
            try {
                //Get inventory item
                return mInventoryConnector.getItems().get(0);

            } catch (RemoteException | ClientException | ServiceException | BindingException e) {
                e.printStackTrace();
            }
            return null;
        }

/*
5) Set the TextView to display the item’s name. For other methods that Item 
   objects have, see 
   https://clover.github.io/clover-android-sdk/com/clover/sdk/v3/inventory
   /Item.html 
*/
      
        @Override
        protected final void onPostExecute(Item item) {
            if (item != null) {
                mTextView.setText(item.getName());
            }
        }
    }
}

Now you can sideload your app onto your device to see it in action. For more information, see Sideloading an APK.


Did this page help you?