Setup SDK

Android TV PacketSDK Integration Guide

Overview

This guide is intended for application developers who want to monetize their Android apps using the Packet SDK. Packet SDK supports Android native apps written in Kotlin and Java.

Integrating the Packet SDK into your app is the first step to earning revenue. Once integrated, you’ll start seeing revenue in the Packet SDK dashboard within 24 hours.

This document will walk you through the steps required for proper integration. Most apps can be integrated with the Packet SDK in about 15 to 20 minutes.

Compatibility Information

Category
Compatibility

CPU

armeabi, x86, armeabi-v7a, x86_64, arm64-v8a

System

compatible with Android 5.0 and above

Development Environment

Recommended to use Android Studio for development

Preparation

Apply for appkey

Register and login the PacketSDK, go to Dashboard>APP to add your app and get your appkey.

SDK Integration Package Directory Structure

After completing the previous step, download the Android SDK package packet_sdk_android_tv.zip.

Unzip the SDK package to find the following files:

├── SDKDemo: A demo app with the SDK already integrated. You can use this demo to understand how to integrate the SDK. Make sure to test the demo first to understand how the SDK works.

├── libs

│ └── packet_sdk_tv_v***.aar: The obfuscated AAR file.

Installation

  1. Copy packet_sdk_tv_v***.aar to the libs directory of your project.

    └── packet_sdk_tv_v***.aar
  2. Set the libs path in the gradle file.

dependencies {
    implementation(files("libs/packet_sdk_tv_v***.aar"))
}

Example files structure:

my-application
├── app
│   ├── libs
│   │   ├── packet_sdk_v***.aar
│   ├── src
│   │   ├── main
│   │   └── AndroidManifest.xml
│   └── build.gradle.kts
├── gradle.properties
├── gradlew
...

SDK setup

SDK requires internet connectivity to work. To allow your app to use internet connection add following permission to AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET" \>

Configure SDK settings right after initializing the SDK, but before calling the PacketSdk.start() function.

package com.example.app

import android.app.Application
import com.packet.sdk.PacketSdk

class App : Application() {

    override fun onCreate() {
        super.onCreate()
        PacketSdk.initialize(this, "app_key")
        // SDK configuration goes here
    }
}

Starting SDK

package com.example.app

import android.app.Activity
import com.packet.sdk.PacketSdk

class MainActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (PacketSdk.isOptedIn()) {
            PacketSdk.start()
        } else {
            PacketSdk.requestConsent(this)
        }
    }
}

Stop SDK

Call PacketSdk.stop() in order to stop SDK.

User should be able to opt out from using SDK at any time. When user does so within your app, you should call PacketSdk.optOut() function, which will immediately stop SDK and will no longer start it unless user grants consent again.

Running in background

If you want to keep SDK running in background when app is closed, you need to take some additional steps. A few additional FOREGROUND_SERVICE permission are required. Add them to your AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.INTERNET" />

    <!-- Permissions for background functionality -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

    <application ...
    </application>
</manifest>

Then let SDK know that you want to allow it to run in the background.

package com.example.app

import android.app.Application
import com.packet.sdk.PacketSdk

class App : Application() {

    override fun onCreate() {
        super.onCreate()
        PacketSdk.initialize(this, "app_key")
        //SDK configuration goes here
        //Runing in background
        PacketSdk.setBackground(true)

        PacketSdk.setNotification(yourNotification)
        PacketSdk.setNotificationId(yourNotificationId)
    }
}

If default SDK consent window doesn't suit your needs, you can create a custom consent window. Make sure to call PacketSdk.optIn() whenever user gives a consent and PacketSdk.optOut() whenever user declines it.

package com.example.app

import android.app.Activity
import com.packet.sdk.PacketSdk

class MainActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // ...
        acceptButton.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                PacketSdk.optIn()
                PacketSdk.start()
            }
        })
        declineButton.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                PacketSdk.optOut()
            }
        })
    }
}

Logging

Call PacketSdk.setEnableLogging(true) in order to enable logging to logcat.

Status monitor

package com.example.app

import android.app.Activity
import com.packet.sdk.PacketSdk

class MainActivity : Activity() {
    private var tvContent: TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        tvContent = findViewById(R.id.tv_content)
        val stringBuilder = StringBuilder()
        PacketSdk.setStatusListener { code: Int, msg: String? ->
            stringBuilder.append("code: ").append(code).append(", msg: ").append(msg).append("\r\n")
            runOnUiThread { tvContent!!.text = stringBuilder }
        }
        // ...
    }
}

SDK API

package com.packet.sdk;

import android.app.Notification;
import android.content.Context;

public class PacketSdk {
    /**
     * First function to call for SDK. Prepares SDK for configuration and use.
     * Uses [context] to prepare SDK for use.
     * appKey.
     */
    public static void initialize(Context context, String appKey);

    /**
     * Sets whether to enable logcat logging
     * Tag is PacketSdk
     */
    public static void setEnableLogging(boolean enableLogging);

    /**
     * Sets whether to allow SDK to run in background when app is no longer in focus.
     */
    public static void setBackground(boolean isBackground);

    /**
     * Sets Android notification to be used when SDK is allowed to run in background.
     */
    public static void setNotification(Notification notification);

    /**
     * Sets Android notification id to be used when SDK is allowed to run in background.
     */
    public static void setNotificationId(int id);

    /**
     * Checks whether SDK has been started.
     */
    public static boolean isRunning();

    /**
     * Starts SDK if user has given the consent, otherwise does nothing.
     * Pass [apiKey] that you received via dashboard for your app.
     */
    public static void start();

    /**
     * Stops SDK if it's running.
     */
    public static void stop();

    /**
     * Checks whether user has given the consent to start SDK.
     */
    public static boolean isOptedIn();

    /**
     * Withdraws user's consent and stops SDK if it was running.
     */
    public static void optOut();

    /**
     * Informs SDK that user gave consent. Used with custom consent requests when
     * provided consent UI is insufficient.
     * This does not start SDK, [PacketSdk.start()] still needs to be called.
     */
    public static void optIn();

    /**
     * Starts an activity displaying consent request with default settings.
     */
    public static void requestConsent(Context context);

    /**
     * Starts an activity displaying consent request from user with configurable settings.
     *
     * [textColor] integer representing text color displayed on consent window (argb: 0xFF000000).
     * [linksColor] integer representing clickable text color displayed on background (argb: 0xFF000000).
     * [primaryColor] integer representing primary color of button (argb: 0xFF000000).
     * [secondColor] integer representing second color of button (argb: 0xFF000000).
     */
    public static void requestConsent(
        Context context, 
        int textColor, 
        int linksColor, 
        int primaryColor, 
        int secondColor
    );

    /**
     * Allows to set listener to monitor sdk status.
     */
    public static void setStatusListener(OnStatusListener onStatusListener);
}

Status Value Description

code
msg

100

Starting...

101

Get server fail.

102

Connect fail.

200

Connect success.

Last updated