Android Background Foreground: Difference between revisions

From Engineering Client Portal

No edit summary
No edit summary
 
Line 11: Line 11:
Foreground/Background state measurement may be implemented in multiple ways
Foreground/Background state measurement may be implemented in multiple ways
for Android. This includes
for Android. This includes
* Enable the Nielsen SDK to measure background/foreground state by making
* Enable the Nielsen SDK to measure background/foreground state by making the relevant update to the AndroidManifest.
the relevant update to the AndroidManifest.
* Implement the required methods.
* Implement the required methods.
* Integrate Nielsen’s SdkBgFgDetectionUtility class within your Custom
* Integrate Nielsen’s SdkBgFgDetectionUtility class within your Custom

Latest revision as of 17:10, 8 November 2017

Engineering Portal / Digital / International / Android Background Foreground

Measuring the Android Foreground/Background State

This guide shows you how to integrate the SdkBgFgDetectionUtility class which Nielsen has made available for measurement of Android Foreground/Background state. Correct measurement of foreground/background state is crucial to Static App measurement within Nielsen Digital Content Ratings (DCR).

Foreground and Background State Measurement

Foreground/Background state measurement may be implemented in multiple ways for Android. This includes

  • Enable the Nielsen SDK to measure background/foreground state by making the relevant update to the AndroidManifest.
  • Implement the required methods.
  • Integrate Nielsen’s SdkBgFgDetectionUtility class within your Custom

Application Class. This document is intended to demonstrate how to integrate the SdkBgFgDetectionUtility class. The SdkBgFgDetectionUtility is compatible with Android 4+. If your Android application does not have an existing Custom Application Class, then foreground/background measurement should be achieved through the use of the AndroidManifest solution.

Integrating the Utility Class

There are four main steps to integrating the SdkBgFgDetectionUtility class for use in handling foreground/background state notification.

Add the Class

First copy and paste the SdkBgFgDetectionUtility.java class into your project.

Link the Class to your Project

You must now open SdkBgFgDetectionUtility class and set the package name header to the package name for your specific application:

//Change package to your project package
package com.nielsen.myapplication;

Instantiate the Utility

Instantiate the SdkBgFgDetectionUtility within the onCreate() method of your app’s inherited Application Class.

SdkBgFgDetectionUtility sdkUtility = new SdkBgFgDetectionUtility(this);

Initialise the Class

Call the method handleSdkLifecyleEvents to let the class start handling foreground/background state measurement.

sdkUtility.handleSdkLifecycleEvents();

Sample Application Class Implementation

Below is an example application class implementation.

package com.nielsen.myapplication;
import android.app.Application;

public class myApplicationClass extends Application {
@Override
public void onCreate() {
super.onCreate();
SdkBgFgDetectionUtility sdkUtility = new SdkBgFgDetectionUtility(this);
sdkUtility.handleSdkLifecycleEvents();
}
}

SdkBgFgDetectionUtility.java class

//Change package to your project package
package com.organization.appname;

/**
 * Created by Nielsen SDK integration support team on 08/11/16.
 */

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import com.nielsen.app.sdk.AppLaunchMeasurementManager;
/**
 * Class to handle app launch  measurement with Nielsen SDK. This solution only works with android 4+.
 */
public class SdkBgFgDetectionUtility {
    //Instance of Application class
    private Application app = null;
    /**
     * Constructor for SdkLifeCycleUtility
     * @param appInstance : Instance of Application. This parameter should not be null.
     */
    public SdkBgFgDetectionUtility(Application appInstance){
        this.app = appInstance;
    }

    /**
     * Function to start handling app life cycle events like app background and foreground etc.
     *
     */
    @TargetApi(14)
    public void handleSdkLifecycleEvents(){


        if(Build.VERSION.SDK_INT >= 14 && this.app != null) {
            this.app.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
                private int b = 0;

                public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                }

                public void onActivityDestroyed(Activity activity) {
                }

                public void onActivityPaused(Activity activity) {
                }

                public void onActivityStarted(Activity activity) {
                    if(this.b == 0) {
                        AppLaunchMeasurementManager.appInForeground(app.getApplicationContext());
                        Log.d("SdkBgFgDetectionUtility", "App running in foreground");
                        ++this.b;
                    } else if(this.b > 0) {
                        ++this.b;
                    }

                }

                public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
                }

                public void onActivityResumed(Activity activity) {
                }

                public void onActivityStopped(Activity activity) {
                    --this.b;
                    if(this.b == 0) {
                        AppLaunchMeasurementManager.appInBackground(app.getApplicationContext());
                        Log.d("SdkBgFgDetectionUtility", "App going to background");
                    }

                }
            });
        } else {
            Log.w("SdkBgFgDetectionUtility", "Cannot detect background/foreground states automatically as the android version is below ICS(API Level 14)");
        }
    }

}