Menu

Activate your Knox License

Your app must activate your Knox license to authenticate API access. For more, see Knox licenses.

Set up the License

Create the License Receiver class

In your app, create a new class called SampleLicenseReceiver.java that contains a license receiver for your KPE key.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class SampleLicenseReceiver extends BroadcastReceiver {

 private int DEFAULT_ERROR_CODE = -1;

 private void showToast(Context context, int msg_res) {
  Toast.makeText(context, context.getResources().getString(msg_res), Toast.LENGTH_SHORT).show();
 }

 private void showToast(Context context, String msg) {
  Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
 }

 @Override
 public void onReceive(Context context, Intent intent) {

  int msg_res = -1;

  if (intent == null) {
   // No intent action is available
   showToast(context, R.string.no_intent);
   return;
  } else {
   String action = intent.getAction();
   if (action == null) {
    // No intent action is available
    showToast(context, R.string.no_intent_action);
    return;
   } else if (action.equals(KnoxEnterpriseLicenseManager.ACTION_LICENSE_STATUS)) {
    // Intent from KPE license activation attempt is obtained
    int errorCode = intent.getIntExtra(KnoxEnterpriseLicenseManager.EXTRA_LICENSE_ERROR_CODE, DEFAULT_ERROR_CODE);

    if (errorCode == KnoxEnterpriseLicenseManager.ERROR_NONE) {
     // license activated successfully
     showToast(context, R.string.klm_activated_succesfully);
     Log.d("SampleLicenseReceiver", context.getString(R.string
      .klm_activated_succesfully));
     return;
    } else {
     // license activation failed
     switch (errorCode) {
      case KnoxEnterpriseLicenseManager.ERROR_INTERNAL:
       msg_res = R.string.err_klm_internal;
       break;
      case KnoxEnterpriseLicenseManager.ERROR_INTERNAL_SERVER:
       msg_res = R.string.err_klm_internal_server;
       break;
      case KnoxEnterpriseLicenseManager.ERROR_INVALID_LICENSE:
       msg_res = R.string.err_klm_licence_invalid_license;
       break;
      case KnoxEnterpriseLicenseManager.ERROR_INVALID_PACKAGE_NAME:
       msg_res = R.string.err_klm_invalid_package_name;
       break;
      case KnoxEnterpriseLicenseManager.ERROR_LICENSE_TERMINATED:
       msg_res = R.string.err_klm_licence_terminated;
       break;
      case KnoxEnterpriseLicenseManager.ERROR_NETWORK_DISCONNECTED:
       msg_res = R.string.err_klm_network_disconnected;
       break;
      case KnoxEnterpriseLicenseManager.ERROR_NETWORK_GENERAL:
       msg_res = R.string.err_klm_network_general;
       break;
      case KnoxEnterpriseLicenseManager.ERROR_NOT_CURRENT_DATE:
       msg_res = R.string.err_klm_not_current_date;
       break;
      case KnoxEnterpriseLicenseManager.ERROR_NULL_PARAMS:
       msg_res = R.string.err_klm_null_params;
       break;
      case KnoxEnterpriseLicenseManager.ERROR_UNKNOWN:
       msg_res = R.string.err_klm_unknown;
       break;
      case KnoxEnterpriseLicenseManager.ERROR_USER_DISAGREES_LICENSE_AGREEMENT:
       msg_res = R.string.err_klm_user_disagrees_license_agreement;
       break;

      default:
       // Unknown error code
       String errorStatus = intent.getStringExtra(KnoxEnterpriseLicenseManager.EXTRA_LICENSE_STATUS);
       String msg = context.getResources().getString(R.string.err_klm_code_unknown, Integer.toString(errorCode), errorStatus);
       showToast(context, msg);
       Log.d("SampleLicenseReceiver", msg);
       return;
     }

     // Display error message
     showToast(context, msg_res);
     Log.d("SampleLicenseReceiver", context.getString(msg_res));
     return;
    }
   }
  }
 }
}

Register the License Receiver class

When the KPE license is validated, the com.samsung.android.knox.intent.action.KNOX_LICENSE_STATUS intent is emitted. The app uses the SampleLicenseReceiver class to intercept this intent.

Paste this code in AndroidManifest.xml to define the receiver for com.samsung.android.knox.intent.action.KNOX_LICENSE_STATUS intents:

<receiver android:name=".SampleLicenseReceiver" >
	<intent-filter>
	<action android:name="com.samsung.android.knox.intent.action.KNOX_LICENSE_STATUS" />
	</intent-filter>
</receiver>

When your app receives a com.samsung.android.knox.intent.action.KNOX_LICENSE_STATUS intent, it passes the intent to SampleLicenseReceiver.onReceive(). Use this method to have your app respond to the license activation.

Activate the license

Use this method in your MainActivity to activate the license.

private void activateLicense() {
    // Instantiate the KnoxEnterpriseLicenseManager class to use the activateLicense method
    KnoxEnterpriseLicenseManager licenseManager = KnoxEnterpriseLicenseManager.getInstance(this);

    try {
        // License Activation TODO Add license key to Constants.java
        licenseManager.activateLicense(Constants.KPE_LICENSE_KEY);
        utils.log(getResources().getString(R.string.license_progress));

    } catch (Exception e) {
        utils.processException(e,TAG);
    }
}

When you launch the app for the first time and activate the license, you will see a Samsung confirmation dialog. This only appears once per license activation. Agree to the terms and conditions and tap CONFIRM.

There are three terms that are important when it comes to licenses that you can receive in the KnoxEnterpriseLicenseManager.ACTION_LICENSE_STATUS broadcast:

  1. Activation - Initial license activation (Constant is integer 800)
  2. Deactivation - License deactivation (Constant is integer 802)
  3. Validation - Once or twice a day, the license agent checks to see if the license is still valid. For example, if it expires before a validation check is made, the license agent returns a value that indicates license expiration. (Constant is integer 801)

Below is the block of code from the Broadcast receiver that catches the license activation:

if (action.equals(KnoxEnterpriseLicenseManager.ACTION_LICENSE_STATUS)) {
	String status = intent.getStringExtra(KnoxEnterpriseLicenseManager.EXTRA_LICENSE_STATUS);
	int errorCode = intent.getIntExtra(KnoxEnterpriseLicenseManager.EXTRA_LICENSE_ERROR_CODE, 1);
	int extraResult = intent.getIntExtra(KnoxEnterpriseLicenseManager.EXTRA_LICENSE_RESULT_TYPE, 1);
	toast.setText("KLM Status = " + status + ",  " + "Error Code= " + errorCode + ",  " + "Extra Result Type= " + extraResult);
	toast.show();
	Log.d(TAG, "KLM Status = " + status + ",  " + "Error Code= " + errorCode + ",  " + "Extra Result Type= " + extraResult);
}

For example, if a validation request is made and passes, the following toast appears:

KLM Status = Success, Error Code = 0, Extra Result Type = 801

Create the License Receiver class

In your app, create a new class called SampleLicenseReceiver.java, which contains a license receiver for your KPE and backwards-compatible keys.

The KnoxEnterpriseLicenseManager class is used to receive the KPE key, while the EnterpriseLicenseManager class is used to receive the backwards-compatible key.

package com.samsung.knox.example.compatibility;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.samsung.android.knox.license.EnterpriseLicenseManager;
import com.samsung.android.knox.license.KnoxEnterpriseLicenseManager;


public class SampleLicenseReceiver extends BroadcastReceiver {

	private int DEFAULT_ERROR_CODE = -1;

	private void showToast(Context context, int msg_res) {
		Toast.makeText(context, context.getResources().getString(msg_res), Toast.LENGTH_SHORT).show();
	}

	private void showToast(Context context, String msg) {
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onReceive(Context context, Intent intent) {

		int msg_res = -1;

		if (intent == null) {
			// No intent action is available
			showToast(context, R.string.no_intent);
			return;
		} else {
			String action = intent.getAction();
			if (action == null) {
				// No intent action is available
				showToast(context, R.string.no_intent_action);
				return;
			}  else if (action.equals(KnoxEnterpriseLicenseManager.ACTION_LICENSE_STATUS)) {
			// KPE activation result Intent is obtained
			int errorCode = intent.getIntExtra(KnoxEnterpriseLicenseManager.EXTRA_LICENSE_ERROR_CODE, DEFAULT_ERROR_CODE);

			if (errorCode == KnoxEnterpriseLicenseManager.ERROR_NONE) {
				// KPE activated successfully
				showToast(context, R.string.klm_activated_successfully);
				Log.d("SampleLicenseReceiver", context.getString(R.string.klm_activated_successfully));
				return;
			} else {
				// KPE activation failed
				switch (errorCode) {
					case KnoxEnterpriseLicenseManager.ERROR_INTERNAL:
						msg_res = R.string.err_klm_internal;
						break;
					case KnoxEnterpriseLicenseManager.ERROR_INTERNAL_SERVER:
						msg_res = R.string.err_klm_internal_server;
						break;
					case KnoxEnterpriseLicenseManager.ERROR_INVALID_LICENSE:
						msg_res = R.string.err_klm_licence_invalid_license;
						break;
					case KnoxEnterpriseLicenseManager.ERROR_INVALID_PACKAGE_NAME:
						msg_res = R.string.err_klm_invalid_package_name;
						break;
					case KnoxEnterpriseLicenseManager.ERROR_LICENSE_TERMINATED:
						msg_res = R.string.err_klm_licence_terminated;
						break;
					case KnoxEnterpriseLicenseManager.ERROR_NETWORK_DISCONNECTED:
						msg_res = R.string.err_klm_network_disconnected;
						break;
					case KnoxEnterpriseLicenseManager.ERROR_NETWORK_GENERAL:
						msg_res = R.string.err_klm_network_general;
						break;
					case KnoxEnterpriseLicenseManager.ERROR_NOT_CURRENT_DATE:
						msg_res = R.string.err_klm_not_current_date;
						break;
					case KnoxEnterpriseLicenseManager.ERROR_NULL_PARAMS:
						msg_res = R.string.err_klm_null_params;
						break;
					case KnoxEnterpriseLicenseManager.ERROR_UNKNOWN:
						msg_res = R.string.err_klm_unknown;
						break;
					case KnoxEnterpriseLicenseManager.ERROR_USER_DISAGREES_LICENSE_AGREEMENT:
						msg_res = R.string.err_klm_user_disagrees_license_agreement;
						break;
					default:
						// Unknown error code
						String errorStatus = intent.getStringExtra(KnoxEnterpriseLicenseManager.EXTRA_LICENSE_STATUS);
						String msg = context.getResources().getString(R.string.err_klm_code_unknown, Integer.toString(errorCode), errorStatus);
						showToast(context, msg);
						Log.d("SampleLicenseReceiver", msg);
						return;
				}

				// Display error message
				showToast(context, msg_res);
				Log.d("SampleLicenseReceiver", context.getString(msg_res));
				return;
			}

		} else if (action.equals(EnterpriseLicenseManager.ACTION_LICENSE_STATUS)) {
			// Backwards-compatible key activation result Intent is obtained
			int errorCode = intent.getIntExtra(EnterpriseLicenseManager.EXTRA_LICENSE_ERROR_CODE, DEFAULT_ERROR_CODE);

			if (errorCode == EnterpriseLicenseManager.ERROR_NONE) {
				// Backwards-compatible key activated successfully
				showToast(context, R.string.elm_activated_succesfully);
				Log.d("SampleLicenseReceiver", context.getString(R.string.elm_activated_succesfully));
				return;
			} else {
				// Backwards-compatible key activation failed
				switch (errorCode) {
					case EnterpriseLicenseManager.ERROR_INTERNAL:
						msg_res = R.string.err_elm_internal;
						break;
					case EnterpriseLicenseManager.ERROR_INTERNAL_SERVER:
						msg_res = R.string.err_elm_internal_server;
						break;
					case EnterpriseLicenseManager.ERROR_INVALID_LICENSE:
						msg_res = R.string.err_elm_licence_invalid_license;
						break;
					case EnterpriseLicenseManager.ERROR_INVALID_PACKAGE_NAME:
						msg_res = R.string.err_elm_invalid_package_name;
						break;
					case EnterpriseLicenseManager.ERROR_LICENSE_TERMINATED:
						msg_res = R.string.err_elm_licence_terminated;
						break;
					case EnterpriseLicenseManager.ERROR_NETWORK_DISCONNECTED:
						msg_res = R.string.err_elm_network_disconnected;
						break;
					case EnterpriseLicenseManager.ERROR_NETWORK_GENERAL:
						msg_res = R.string.err_elm_network_general;
						break;
					case EnterpriseLicenseManager.ERROR_NOT_CURRENT_DATE:
						msg_res = R.string.err_elm_not_current_date;
						break;
					case EnterpriseLicenseManager.ERROR_NULL_PARAMS:
						msg_res = R.string.err_elm_null_params;
						break;
					case EnterpriseLicenseManager.ERROR_SIGNATURE_MISMATCH:
						msg_res = R.string.err_elm_sig_mismatch;
						break;
					case EnterpriseLicenseManager.ERROR_UNKNOWN:
						msg_res = R.string.err_elm_unknown;
						break;
					case EnterpriseLicenseManager.ERROR_USER_DISAGREES_LICENSE_AGREEMENT:
						msg_res = R.string.err_elm_user_disagrees_license_agreement;
						break;
					case EnterpriseLicenseManager.ERROR_VERSION_CODE_MISMATCH:
						msg_res = R.string.err_elm_ver_code_mismatch;
						break;
					default:
						// Unknown error code
						String errorStatus = intent.getStringExtra(EnterpriseLicenseManager.EXTRA_LICENSE_STATUS);
						String msg = context.getResources().getString(R.string.err_elm_code_unknown, Integer.toString(errorCode), errorStatus);
						showToast(context, msg);
						Log.d("SampleLicenseReceiver", msg);
						return;
				}

				// Display backwards-compatible key error message
				showToast(context, msg_res);
				Log.d("SampleLicenseReceiver", context.getString(msg_res));
				return;
			}
		}
	}
}
					

Register the License Receiver class

For devices running Knox versions 2.5 to 2.7.1 / Knox API 17 to 21 will also need to activate a backwards-compatible license key with a KPE license key. When the KPE key is validated, the com.samsung.android.knox.intent.action.KNOX_LICENSE_STATUS intent is emitted. When the backwards-compatible key is validated, the com.samsung.android.knox.intent.action.LICENSE_STATUS intent is emitted. The app uses the SampleLicenseReceiver class to intercept this intent.

Paste this code in AndroidManifest.xml to define the receiver for com.samsung.android.knox.intent.action.LICENSE_STATUS intents:

<receiver android:name=".SampleLicenseReceiver">
	<intent-filter>
	<action android:name="com.samsung.android.knox.intent.action.KNOX_LICENSE_STATUS" />
	<action android:name="com.samsung.android.knox.intent.action.LICENSE_STATUS" />
	</intent-filter>
</receiver>

When your app receives the com.samsung.android.knox.intent.action.KNOX_LICENSE_STATUS and the com.samsung.android.knox.intent.action.LICENSE_STATUS intents, it passes the intents to SampleLicenseReceiver.onReceive(). Use this method to have your app respond to the license activation.

Activate the license

Use this method in your MainActivity to activate the license.

private void activateLicense() {

    // Instantiate the KnoxEnterpriseLicenseManager class to use the activateLicense method
    KnoxEnterpriseLicenseManager licenseManager = KnoxEnterpriseLicenseManager.getInstance(this);

    try {
        // License Activation TODO Add license key to Constants.java
        licenseManager.activateLicense(Constants.KPE_LICENSE_KEY);
        utils.log(getResources().getString(R.string.license_progress));

    } catch (Exception e) {
        utils.processException(e,TAG);
    }
}


// call backwards-compatible key activation
private void activateBackwardsCompatibleKey() {

    // Get an instance of the License Manager
    EnterpriseLicenseManager BackwardsCompatibleKeyManager = EnterpriseLicenseManager.getInstance(this);

    try {
        // Activate the backwards-compatible license key
        BackwardsCompatibleKeyManager.activateLicense(Constants.BACKWARDS_COMPATIBLE_KEY);
        utils.log(getResources().getString(R.string.backwards_compatible_key_activation));

    } catch (Exception e) {
        utils.processException(e, TAG);
    }
}

When you launch the app for the first time and activate the license, you will see a Samsung confirmation dialog. This only appears once per license activation. Agree to the terms and conditions and tap CONFIRM.

There are three terms that are important when it comes to licenses that you can receive in the KnoxEnterpriseLicenseManager.ACTION_LICENSE_STATUS broadcast:

  1. Activation - Initial license activation (Constant is integer 800)
  2. Deactivation - License deactivation (Constant is integer 802)
  3. Validation - Once or twice a day, the license agent checks to see if the license is still valid. For example, if it expires before a validation check is made, the license agent returns a value that indicates license expiration. (Constant is integer 801)

Below is the block of code from the Broadcast receiver that catches the license activation:

if (action.equals(KnoxEnterpriseLicenseManager.ACTION_LICENSE_STATUS)) {
	String status = intent.getStringExtra(KnoxEnterpriseLicenseManager.EXTRA_LICENSE_STATUS);
	int errorCode = intent.getIntExtra(KnoxEnterpriseLicenseManager.EXTRA_LICENSE_ERROR_CODE, 1);
	int extraResult = intent.getIntExtra(KnoxEnterpriseLicenseManager.EXTRA_LICENSE_RESULT_TYPE, 1);
	toast.setText("KLM Status = " + status + ",  " + "Error Code= " + errorCode + ",  " + "Extra Result Type= " + extraResult);
	toast.show();
	Log.d(TAG, "KLM Status = " + status + ",  " + "Error Code= " + errorCode + ",  " + "Extra Result Type= " + extraResult);
}

For example, if a validation request is made and passes, the following toast appears:

KLM Status = Success, Error Code = 0, Extra Result Type = 801

See also:

Tutorial Progress

You are 4/7 done! Go to the next step.