For the complete documentation index, see llms.txt. This page is also available as Markdown.

Android

Initialization allows the SDK to perform detections in the background. You must always initialize the SDK before interacting with it. Only a limited set of SDK methods are safe to invoke on an uninitialized SDK.

Steps

1

Create an Application Class

Initialization must be done in the onCreate() method of your Application class. If you don't yet have a custom application class, first create a new class that extends Application.

package com.example.myapp

import android.app.Application

class MyApplication: Application() {
    override fun onCreate() {
        super.onCreate()
    }
}

Then reference this new class in the application tag of the AndroidManifest.xml

AndroidManifest.xml
<application
        android:name="com.example.myapp.MyApplication"
        ...
        >
        ...
</application>
2

Initialize the SDK

In the onCreate() method of your Application class, call initializeAsync().

package com.example.myapp

import android.app.Application
import android.util.Log
import com.sentiance.sdk.Sentiance

class MyApplication: Application() {
    override fun onCreate() {
        super.onCreate()
        
        val options = SentianceOptions.Builder(this).build()
        Sentiance.getInstance(this).initializeAsync(options)
            .addOnCompleteListener { operation ->
                if (operation.isSuccessful) {
                    Log.d(TAG, "Initialization succeeded");
                } else {
                    val reason = operation.error.failureReason.name
                    val throwable = operation.error.throwable
                    Log.e(TAG, "Intialization failed with reason ${reason}", throwable);
                }
            }
    }
    
    companion object {
        private const val TAG = "Sentiance SDK"
    }
}
3

Customize the Android Notification (Optional)

When the Sentiance SDK runs in the background, it starts a foreground service. This causes Android to display a notification in the notification shade, and an icon on the system bar.

You can customize this notification to tailor its appearance and behavior to match the branding and user experience of your application. This allows you to provide a seamless and consistent experience to your users while the Sentiance SDK operates in the background.

Please note that customizing the notification should be done carefully to ensure that it complies with Android guidelines and user preferences. By doing so, you can enhance the overall user experience and maintain a cohesive presentation of your app and the Sentiance SDK's background functionality.

You can specify the notification that Android should use via the SDK's initialization option:

override fun onCreate() {
    super.onCreate()

    val options = SentianceOptions.Builder(this)
        .setNotification(notification, notificationId)
        .build()
    Sentiance.getInstance(this).initializeAsync(options)
        ..
}

If the notificationId that you specify here matches the ID of another notification, Android will show only one notification (the last published one). If your app has an ongoing notification of its own, you can pass the same ID to avoid multiple notifications during SDK detections.

Be sure to check our notification management page for more on the best practices of defining a service notification.

4

Next: User Creation

After having successfully initialized the Sentiance SDK, you must now create a user. To do so, follow the instructions on this page.

Last updated