If you have not completed the initial integration step, refer to How to Integrate the Device Data Collector into Mobile Apps using Android Gradle (Java) or Integrate the Device Data Collector into Native Java Android Applications using the Library JAR depending on your integration type.
This guide is for a Native Android Kotlin application implementation of the SDK. This guide provides instructions on how to add the SDK into the application and have it collect information about the device.
-
To enable INTERNET access so the collector can transmit information back to Equifax and Location Collection support to detect devices in suspicious locations, add the following permissions to your application’s XML under the <manifest> tag:
<manifest> .... <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> </manifest> -
You can enable Analytics support in two ways:
-
For customers who do not use a custom application class
Enable support by adding the following code to your application’s
AndroidManifest.xmlunder the<application>tag.<application android:name="com.kount.api.initialization.KountAnalyticsApplication" ...> ... </application>
-
For customers who use a custom application class
Enable Analytics Collection support by adding
Application.ActivityLifecycleCallbacksto your application class.import com.kount.api.initialization.AnalyticsApplication.registerKountEvents import com.kount.api.internal.analytics.EventEnums class CustomerApplication : Application(), ActivityLifecycleCallbacks { override fun onCreate() { super.onCreate() registerActivityLifecycleCallbacks(this) } override fun onActivityCreated(activity: Activity, bundle: Bundle?) { Log.d(TAG, EventEnums.EVENT_CREATED.toString() + " for " + activity.getLocalClassName()) registerKountEvents(EventEnums.EVENT_CREATED, activity) } override fun onActivityStarted(activity: Activity) { Log.d(TAG, EventEnums.EVENT_STARTED.toString() + " for " + activity.getLocalClassName()) registerKountEvents(EventEnums.EVENT_STARTED, activity) } override fun onActivityResumed(activity: Activity) { Log.d(TAG, EventEnums.EVENT_RESUMED.toString() + " for " + activity.getLocalClassName()) registerKountEvents(EventEnums.EVENT_RESUMED, activity) } override fun onActivityPaused(activity: Activity) { Log.d(TAG, EventEnums.EVENT_PAUSED.toString() + " for " + activity.getLocalClassName()) registerKountEvents(EventEnums.EVENT_PAUSED, activity) } override fun onActivityStopped(activity: Activity) { Log.d(TAG, EventEnums.EVENT_STOPPED.toString() + " for " + activity.getLocalClassName()) registerKountEvents(EventEnums.EVENT_STOPPED, activity) } override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) { } override fun onActivityDestroyed(activity: Activity) { } companion object { private const val TAG = "CustomerApplication" } }
-
-
In the main activity of your application, you must assign the
<MERCHANT_ID>you received.Optionally, you can provide a sessionID if you need to generate your own (if not, the SDK generates one for you). You must send this sessionID back to your servers in order to make calls to the API. Add the following code in your
MainActivity: -
Add the following initialization to your MainActivity.
import com.kount.api.KountSDK; class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback { var deviceSessionID: String? = "" val KEY_UUID: String = "UUID" val MERCHANT_ID: String = "999999" // Insert your valid merchant ID. override fun onCreate(savedInstanceState: Bundle?) { ... // Set your merchant ID. KountSDK.setMerchantId(MERCHANT_ID) // This turns the alpha collections on(true)/off(false). It defaults to true KountSDK.setCollectAnalytics(true) // Set the environment for the SDK. KountSDK.setEnvironment(KountSDK.ENVIRONMENT_TEST) if (savedInstanceState == null) { /** If you want to pass in a self generated sessionID(or one given * to you by your servers) you can set it here instead of generating a UUID using the code below. * Make sure you set session ID only one time in a user session by saving it in the savedInstanceState. * An example to create a sessionID is below. */ deviceSessionID = UUID.randomUUID().toString().replace("-", "") KountSDK.setSessionId(deviceSessionID!!) } else { deviceSessionID = savedInstanceState.getString(KEY_UUID) KountSDK.setSessionId(deviceSessionID!!) } //Request location permission for Android 6.0 & above if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // Check for location permissions so the Data Collector can gather the device location Utils.requestLocationPermission(this) } else { // Collect the device data for the session. The session ID will be returned in the success callback. KountSDK.collectForSession(this, { sessionId: String? -> Log.d("TAG", "Client success completed with sessionId $sessionId") Log.d("TAG", "DDC status is ${KountSDK.getCollectionStatus()}") null }, { sessionId: String?, error: KountSdkError? -> Log.d("TAG", "client failed with sessionId $sessionId, $error") Log.d("TAG", "DDC status is ${KountSDK.getCollectionStatus()}") null }) } super.onCreate(savedInstanceState) } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) outState.putString(KEY_UUID, deviceSessionID) } } -
Your MainActivity should implement
ActivityCompat.OnRequestPermissionsResultCallbackand override methodonRequestPermissionsResult(requestCode, permissions, grantResults).override fun onRequestPermissionsResult( requestCode: Int, permissions: Array<out String>, grantResults: IntArray ) { if (requestCode == KountSDK.REQUEST_PERMISSION_LOCATION) { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //This block executes when permission is already granted. KountSDK.collectForSession( this, { sessionId: String? -> Log.d("TAG", "Client success completed with sessionId $sessionId") Log.d("TAG", "DDC status is ${KountSDK.getCollectionStatus()}") null }, { sessionId: String?, error: KountSdkError? -> Log.d("TAG", "Client failed with sessionId $error, $sessionId") Log.d("TAG", "DDC status is $KountSDK.getCollectionStatus()") null } ) } } super.onRequestPermissionsResult(requestCode, permissions, grantResults) } -
To support location collection in Android API 23 and later, add the following code to your main activity,
onCreatemethod.fun requestLocationPermission(activity: Activity) { if (ContextCompat.checkSelfPermission( activity, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) { ActivityCompat.requestPermissions( activity, arrayOf<String>((Manifest.permission.ACCESS_FINE_LOCATION)), REQUEST_PERMISSION_LOCATION ) } else { //This block executes when permission is already granted. KountSDK.collectForSession( this, { sessionId: String? -> Log.d("TAG", "Client success completed with sessionId $sessionId") Log.d("TAG", "DDC status is ${KountSDK.getCollectionStatus()}") }, { sessionId: String?, error: KountSdkError? -> Log.d("TAG", "Client failed with sessionId $error, $sessionId") Log.d("TAG", "DDC status is $KountSDK.getCollectionStatus()") } ) } } -
Almost all API calls require your assigned merchantID and a sessionID. Send the sessionID created earlier back to your servers. This can be done at any time prior to the API calls being made (from your servers to ours). For example, if you are using our services and plan to evaluate logins, you could include the sessionID along with the user's login credentials to your servers. This sessionID is accessible as a global function.
final String sessionId = KountSDK.getSessionId(); // TODO add sessionId in posting to server here
-
Build and run.
Note
Compilation problems? If
android:allowbackup attributeis false in your<application>tag, addtools:replace="android:allowBackup"to your application tag.