Menu

Activate your Knox License

The following instructions describe how to activate a license key within your app. If you want to read about how license keys work, see the License key page.

Set up the License

Create the License Receiver class

In your app, create a new class called SampleLicenseReceiver.java. This 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_license_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_license_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.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.LICENSE_STATUS" />
    </intent-filter>
</receiver>

When your app receives a com.samsung.android.knox.intent.action.LICENSE_STATUS intent, it passes the intent to SampleLicenseReceiver.onReceive(). This method is where you can have your app respond to the license being activated.

Activate the license / deactivate

Use these methods to activate / deactivate 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.LICENSE_KEY);
    logView(getResources().getString(R.string.license_progress));
    } catch (Exception e) {
        processException(e);
    }
}

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 this license is still valid. For example if it expires, the license agent will let you know that the license is now expired (Constant is integer 801)

Here's 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);
}

So for example, if its a validation request and it passes, the resulting toast would look like:

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. This contains a license receiver for your KPE and backwards-compatible key.

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 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 is obtained
        int errorCode = intent.getIntExtra(KnoxEnterpriseLicenseManager.EXTRA_LICENSE_ERROR_CODE, DEFAULT_ERROR_CODE);
        if (errorCode == KnoxEnterpriseLicenseManager.ERROR_NONE) {
          // KPE license activated successfully
          showToast(context, R.string.klm_activated_successfully);
          Log.d("SampleLicenseReceiver", context.getString(R.string.klm_activated_successfully));
          return;
        } else {
          // KPE 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_license_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_license_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_license_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_license_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

When the KPE and backwards-compatible key are 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.LICENSE_STATUS"/>
	</intent-filter>
</receiver>
				

When your app receives a com.samsung.android.knox.intent.action.LICENSE_STATUS intent, it passes the intent to SampleLicenseReceiver.onReceive(). This method is where you can have your app respond to the license being activated.

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);
  EnterpriseLicenseManager backwardsCompatibleKeyManager = EnterpriseLicenseManager.getInstance(this);

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

  } catch (Exception e) {
    processException(e);
  }
}

private void ActivateBackwardsCompatibleKey() {
  try {
    // License Activation TODO Add license key to Constants.java
    backwardsCompatibleKeyManager.activateLicense(Constants.BACKWARDS_COMPATIBLE_KEY);
    logView(getResources().getString(R.string.license_progress));

  } catch (Exception e) {
    processException(e);
  }
}

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 this license is still valid. For example if it expires, then a validation check is made, the license agent will let you know that the license is now expired (Constant is integer 801)

Here's 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);
}

So for example, if its a validation request and it passes, the resulting toast would look like:

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

Declare Knox permissions

You must also declare your Knox permissions in the Android Manifest before the APIs will work.

Get Knox Permissions.