Code Examples

Check out some useful snippets and code examples for initialization below.

package com.example.testapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.preference.PreferenceManager
import com.liveramp.ats.LRAtsManager
import com.liveramp.ats.LRError
import com.liveramp.ats.callbacks.LRCompletionHandlerCallback
import com.liveramp.ats.callbacks.LREnvelopeCallback
import com.liveramp.ats.model.*

class MainActivity : AppCompatActivity() {
           
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // set fake consent (for testing purposes only!), this should be handled by CMP SDK
        setSharedPreferencesKey(
            "IABTCF_VendorConsents",
            "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"
        )
        setSharedPreferencesKey("IABTCF_PurposeConsents", "1111111111")
        // init ATS SDK
        LRAtsManager.initialize(
            LRAtsConfiguration("40b867f9-93cc-4687-a2c7-d02bed91aaee", false, false),
            object : LRCompletionHandlerCallback {
                override fun invoke(success: Boolean, error: LRError?) {
                    if (success) {
                        // get envelope for email
                        LRAtsManager.getEnvelope(LREmailIdentifier("[email protected]"), object : LREnvelopeCallback {
                            override fun invoke(envelope: Envelope?, error: LRError?) {
                                // use envelope here
                            }
                        })
                        // or phone number
                        LRAtsManager.getEnvelope(LRPhoneIdentifier("+14842918944"), object : LREnvelopeCallback {
                            override fun invoke(envelope: Envelope?, error: LRError?) {
                                // use envelope here
                            }
                        })
                        // or for id
                        LRAtsManager.getEnvelope(LRCustomIdentifier("54321:abc123"), object : LREnvelopeCallback {
                            override fun invoke(envelope: Envelope?, error: LRError?) {
                                // use envelope here
                            }
                        })
                    }
                }
            })
    }

    private fun setSharedPreferencesKey(key: String, value: String) {
        val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
        val editor = preferences.edit()
        editor.putString(key, value)
        editor.apply()
    }
}
package com.liveramp.atstestapp;

import android.content.SharedPreferences;
import android.os.Bundle;
import com.liveramp.ats.LRAtsManager;
import com.liveramp.ats.LRError;
import com.liveramp.ats.callbacks.LRCompletionHandlerCallback;
import com.liveramp.ats.callbacks.LREnvelopeCallback;
import com.liveramp.ats.model.Envelope;
import com.liveramp.ats.model.LRAtsConfiguration;
import com.liveramp.ats.model.LRCustomerIdentifier;
import com.liveramp.ats.model.LREmailIdentifier;
import com.liveramp.ats.model.LRPhoneIdentifier;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;

public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // set fake consent (for testing purposes only!), this should be handled by CMP SDK
        setSharedPreferencesKey(
                "IABTCF_VendorConsents",
                "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"
        );
        setSharedPreferencesKey("IABTCF_PurposeConsents", "1111111111");
        // init ATS SDK
        LRAtsManager.INSTANCE.initialize(new LRAtsConfiguration("40b867f9-93cc-4687-a2c7-d02bed91aaee", false, false), new LRCompletionHandlerCallback() {
            @Override
            public void invoke(boolean success, @Nullable LRError lrError) {
                if (success) {
                    // get envelope for email
                    LRAtsManager.INSTANCE.getEnvelope(new LREmailIdentifier("[email protected]"), new LREnvelopeCallback() {
                        @Override
                        public void invoke(@Nullable Envelope envelope, @Nullable LRError lrError) {
                            // use envelope here
                        }
                    });
                    // or phone number
                    LRAtsManager.INSTANCE.getEnvelope(new LRPhoneIdentifier("+14842918944"), new LREnvelopeCallback() {
                        @Override
                        public void invoke(@Nullable Envelope envelope, @Nullable LRError lrError) {
                            // use envelope here
                        }
                    });
                    // or for id
                    LRAtsManager.INSTANCE.getEnvelope(new LRCustomIdentifier("54321:abc123"), new LREnvelopeCallback() {
                        @Override
                        public void invoke(@Nullable Envelope envelope, @Nullable LRError lrError) {
                            // use envelope here
                        }
                    });
                }
            }
        });
    }

    private void setSharedPreferencesKey(String key, String value) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.apply();
    }
}