Examples

Below is a full example implementation of the Playtime SDK. It uses Jetpack Compose to set up a button that leads the user to the Playtime Offerwall. To run the example:

  1. Add the dependency

  2. Add your SDK hash

  3. If you're using an emulator, add it as a test device.

In the future we will update this page with examples for our other wrappers. Please check back soon.

package com.example.sdk_integration_activity

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.adjoe.sdk.Adjoe
import io.adjoe.sdk.Adjoe.getUserId
import io.adjoe.sdk.AdjoeException
import io.adjoe.sdk.AdjoeExtensions
import io.adjoe.sdk.AdjoeGender
import io.adjoe.sdk.AdjoeInitialisationListener
import io.adjoe.sdk.AdjoeNotInitializedException
import io.adjoe.sdk.AdjoeOfferwallListener
import io.adjoe.sdk.AdjoeParams
import io.adjoe.sdk.AdjoeUserProfile
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.Calendar
import java.util.Date

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initAdjoe()
        setupAdjoeOfferwallListener()
    }

// the initAdjoe() function has the adjoe params, but if your params have
// changed, or if you don't yet have a param, it is recommended that you take a
// different approach. For example, if you don't get the user ID until after the
// user has signed in. You should then send these values in the OnResume.
private fun initAdjoe()
{
    // pass in the user id. These values should come from your backend.
    // If you don't get the userID until after the sign-in, this value can be passed-in onResume.
    val userId = "f0b9d695-405d-4b13-8a79-5fe21dc901e6"

    // set up the parameters, these values should also come from your backend.
    val adjoeParams = AdjoeParams.Builder()
        .setUaNetwork("tiktok")
        .setUaChannel("video")
        .setUaSubPublisherCleartext("Example: Game 2")
        .setUaSubPublisherEncrypted("8bb1e7911818be32449f6726ff7ecd102ba1862b")
        .setPlacement("Main Screen")
        .build()

    // these values are additional options that you can pass-in to identify users. These data will be sent back in the S2S URL. Example data below.
    val adjoeExtensions = AdjoeExtensions.Builder()
        .setSubId1("Target Group 1")
        .setSubId2("Target Group 2")
        .build()

    //Set the options
    val options = Adjoe.Options()
        .setUserId(userId)
        .setParams(adjoeParams)
        .setUserProfile(getUserProfile())
        .setExtensions(adjoeExtensions)

    // Initialize the adjoe Playtime SDK, passing in the adjoe Options
    try {
        Adjoe.init(this, "your_sdk_hash", options, object : AdjoeInitialisationListener {
            override fun onInitialisationFinished() {
                // The adjoe Playtime SDK SDK was initialized successfully
                //("initialized successfully")
            }

            override fun onInitialisationError(exception: Exception?) {
                // An error occurred while initializing the Adjoe SDK.
                // Note that exception might be null
            }
        })
    } catch (e: AdjoeNotInitializedException) {
        // Handle initialization exception
    }
    setContent {
        ExampleApp(this)
    }
}

//Get the user profile information. These data should come from your backend. 
    fun getSampleUserGender(): String {
        return "male"
    }

    //Pass in the user's birthday information if it's available
    //and if they've consented to sharing it
    fun getSampleUserBirthday(): Date {
        val birthday = Calendar.getInstance()
        birthday.set(Calendar.YEAR, 1995)
        birthday.set(Calendar.MONTH, Calendar.JANUARY)
        birthday.set(Calendar.DAY_OF_MONTH, 30)

        return birthday.time
    }

    val birthday = getSampleUserBirthday()

    fun getUserProfile(): AdjoeUserProfile {
        val gender: AdjoeGender = when (getSampleUserGender()) {
            "male" -> AdjoeGender.MALE
            "female" -> AdjoeGender.FEMALE
            else -> AdjoeGender.UNKNOWN
        }
        return AdjoeUserProfile(gender, birthday)
    }

//Set up the listener for when the offerwall is opened or closed. 
    private fun setupAdjoeOfferwallListener() {
        Adjoe.setOfferwallListener(object : AdjoeOfferwallListener {
            override fun onOfferwallOpened(type: String) {
                Log.d("ADJOE", "Offerwall of type '" + type + "' was opened")
            }

            override fun onOfferwallClosed(type: String) {
                Log.d("ADJOE", "Offerwall of type '" + type + "' was closed")
            }
        })
    }

    //Whenever your app comes back into the foreground,
    //you should init the SDK again and send the updated adjoe Params.
    override fun onResume() {
        super.onResume()
        initAdjoe()
    }
}

//Set up the params to send with the teaser. Use this in case you have multiple offerwall
// placements in your app and want to evaluate the effectiveness of each.
val teaserAdjoeParams: AdjoeParams = AdjoeParams.Builder()
    .setPlacement("Main Screen")
    .build()

//Set up the additional methods
private suspend fun additionalChecks(context: Context) {
    withContext(Dispatchers.IO) {
        getVersion()
        getVersionName()
        val tosAccepted = hasAcceptedTOS(context)
        val permissionsAccepted = hasAcceptedUsagePermission(context)
        val userIdRetreived = getUserId(context)
    }
}
suspend fun getVersion() {
    val version: Int? = Adjoe.getVersion()
    Log.d("ADJOE", "Adjoe SDK version is $version")
}

suspend fun getVersionName() {
    val versionName: String? = Adjoe.getVersionName()
    Log.d("ADJOE", "Adjoe SDK version name is $versionName")
}

suspend fun hasAcceptedTOS(context: Context) {
    val accepted: Boolean? = Adjoe.hasAcceptedTOS(context)
    Log.d("ADJOE", "TOS accepted: $accepted")
}

suspend fun hasAcceptedUsagePermission(context: Context) {
    val accepted: Boolean? = Adjoe.hasAcceptedUsagePermission(context)
    Log.d("ADJOE", "Usage permission accepted: $accepted")
}

suspend fun getUserId(context: Context) {
    val userId: String? = Adjoe.getUserId(context)
    Log.d("ADJOE", "User ID is \"$userId\"")
}

// Set up the teaser event function
private fun sendTeaser(context:Context) {
            Adjoe.sendUserEvent(context, Adjoe.EVENT_TEASER_SHOWN, null, teaserAdjoeParams)
        }

@Composable
fun ExampleApp(activity: ComponentActivity) {
    Surface(color = MaterialTheme.colorScheme.background) {
        Column(
            modifier = Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            ShowPlaytimeOfferwallButton(activity)
        }
    }
}

@Composable
fun ShowPlaytimeOfferwallButton(activity: ComponentActivity) {
    val coroutineScope = rememberCoroutineScope()

    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        // Button to navigate to the offerwall screen
        Button(
            onClick = {
                coroutineScope.launch {
                    try {
                        val adjoeOfferwallIntent = Adjoe.getOfferwallIntent(activity)
                        activity.startActivity(adjoeOfferwallIntent)
                    } catch(notInitializedException: AdjoeNotInitializedException) {
                        // Handle not initialized exception
                    } catch(exception: AdjoeException) {
                        // Handle other exceptions
                    }
                    sendTeaser(context = activity.applicationContext) // send the teaser when the user clicks the Offerwall Button. The teaser can also be sent when the user views it.
                    additionalChecks(context = activity.applicationContext) // run the additional checks. 
                }
            },
            modifier = Modifier.size(width = 250.dp, height = 75.dp)
        ) {
            Text("Show Playtime Offerwall")
        }
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    ExampleApp(ComponentActivity())
}

Last updated