How to Invoke the Device Data Collector into Mobile Apps using the Native Android Java SDK

If you have not completed the initial integration step, go 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.

Use this guide as part of a Native Android Java application implementation of the SDK. This guide provides instructions to add the SDK into the application and have it collect information about the device.

Implementation Instructions

  1. 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>
  2. Enable Analytics Collection support. There are two methods to enable analytics support:

    • For customers who do not use a custom application class.

      Enable Analytics Collection support by adding the following code to your application’s AndroidManifest.xml under 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.ActivityLifecycleCallbacks to your application class.

      import com.kount.api.initialization.AnalyticsApplication;
      import com.kount.api.internal.analytics.EventEnums;
      
      public class CustomerApplication extends Application implements Application.ActivityLifecycleCallbacks {
          private static final String TAG = "CustomerApplication";
      
          @Override
          public void onCreate() {
              super.onCreate();
              registerActivityLifecycleCallbacks(this);
          }
      
          @Override
          public void onActivityCreated(@NonNull Activity activity, @Nullable @org.jetbrains.annotations.Nullable Bundle bundle) {
              Log.d(TAG, EventEnums.EVENT_CREATED + " for " + activity.getLocalClassName());
              AnalyticsApplication.registerKountEvents(EventEnums.EVENT_CREATED, activity);
          }
      
          @Override
          public void onActivityStarted(@NonNull Activity activity) {
              Log.d(TAG, EventEnums.EVENT_STARTED + " for " + activity.getLocalClassName());
              AnalyticsApplication.registerKountEvents(EventEnums.EVENT_STARTED, activity);
          }
      
          @Override
          public void onActivityResumed(@NonNull Activity activity) {
              Log.d(TAG, EventEnums.EVENT_RESUMED + " for " + activity.getLocalClassName());
              AnalyticsApplication.registerKountEvents(EventEnums.EVENT_RESUMED, activity);
      
          }
      
          @Override
          public void onActivityPaused(@NonNull Activity activity) {
              Log.d(TAG, EventEnums.EVENT_PAUSED + " for " + activity.getLocalClassName());
              AnalyticsApplication.registerKountEvents(EventEnums.EVENT_PAUSED, activity);
      
          }
      
          @Override
          public void onActivityStopped(@NonNull Activity activity) {
              Log.d(TAG, EventEnums.EVENT_STOPPED + " for " + activity.getLocalClassName());
              AnalyticsApplication.registerKountEvents(EventEnums.EVENT_STOPPED, activity);
      
          }
      
          @Override
          public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {
      
          }
      
          @Override
          public void onActivityDestroyed(@NonNull Activity activity) {
      
          }
      }
      
  3. In the main activity of your application, you must initialize the collector and 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.

  4. Add the following initialization in your MainActivity.

    import com.kount.api.KountSDK;
            
    public class MainActity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback{
      final String deviceSessionID="";
      final String KEY_UUID="UUID";
              
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          ...
    
            // 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)->
                {
                    Log.d("TAG", "Client success completed with sessionId "+sessionId);
                    Log.d("TAG", "DDC status is " + KountSDK.getCollectionStatus());
                    return null;
                },(sessionId, error)->
                {
                    Log.d("TAG", "client failed with sessionId $error, $sessionId");
                    Log.d("TAG", "DDC status is " + KountSDK.getCollectionStatus());
                    return null;
                });
            }
      }
      
      @Override
      protected void onSaveInstanceState(@NonNull Bundle outState) {
          super.onSaveInstanceState(outState);
          outState.putString(KEY_UUID, deviceSessionID);
      }
    }    
  5. Your MainActivity should implement ActivityCompat.OnRequestPermissionsResultCallback and override method onRequestPermissionsResult(requestCode, permissions, grantResults).

    @Override
      public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
          final TextView location = (TextView) findViewById(R.id.location);
          if (requestCode == KountSDK.REQUEST_PERMISSION_LOCATION) {
              if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  location.setText("Allowed");
      
                  // Collect the device data for the session. The session ID will be returned in the success callback.
                  KountSDK.collectForSession(this,(sessionId)->
                  {
                      Log.d("TAG", "onrequest success completed with sessionId "+sessionId);
                      Log.d("TAG", "DDC status is " + KountSDK.getCollectionStatus());
                      return null;
                  },(sessionId, error)->
                  {
                      Log.d("TAG", "onrequest failed with sessionId $error, $sessionId");
                      Log.d("TAG", "DDC status is " + KountSDK.getCollectionStatus());
                      return null;
                  });
              } else {
                  location.setText("Denied");
              }
          }
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
      }     
    
    
  6. To support Location Collection in Android API 23 and later, add the following code to your main activity, onCreate method:

       
    if (ContextCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions( this, new String[]{(Manifest.permission.ACCESS_FINE_LOCATION)},KountSDK.REQUEST_PERMISSION_LOCATION);
    } else {
      //This block executes when permission is already granted.  
      KountSDK.collectForSession(this, sessionId -> {
        //success block
        Log.d(TAG, "Client success completed with sessionId: " + sessionId);
        Log.d(TAG, "DDC status is " + KountSDK.getCollectionStatus());
        return null;
      }, (sessionId, error) -> {
      {
        //error block
        Log.e(TAG, "client failed with sessionId: " + sessionId + ", error: " + error);
        Log.e(TAG, "DDC status is " + KountSDK.getCollectionStatus());
        return null;
      });
    }
    
  7. 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 users 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
  8. Build and run.

    Note

    Compilation Problems? If android:allowbackup attribute is false in your application tag, add tools:replace="android:allowBackup" to your application tag.

Was this article helpful?
0 out of 0 found this helpful