HON KEAT WONG
Aug 03, 2017
4:22 am

Auto License Activation

Hi, I would like to ask is there any way to automatically activate the license key during the apps startup?

Because my apps crashed if I put the license activation command on the apps startup

Samuel Veloso
Aug 03, 2017
6:16 pm

Hi,

There is a way to do so. Basically, you have to implement the following in your app:

  1. Activate device admin on startup, if the app isn't device admin yet.
  2. Upon returning to the activity after activating the app as device admin, activate the license. 
  3. Store a boolean in SharedPreferences that saves whether the license has been activated before (you only have to activate it once).
  4. On startup, activate the license if it hasn't already been done so (can check through SharedPreferences boolean flag).

An example of the implementation:

1) Activate device admin on startup, if the app isn't device admin yet. 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   ...

    boolean deviceAdminState = dpm.isAdminActive(mDeviceAdmin);
    if (deviceAdminState) {
        logToast("Device Admin is already active", "info");
    } else {
        try {
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdmin);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Adding app as an admin to restrict device functionality, password protected");
            startActivityForResult(intent, 1);
        } catch (Exception e) {
            logToast("Exception: " + e, "error");
        }
    }

    ...
}

2) Upon returning to the activity after activating the app as device admin, activate the license. 

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == 1) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            activateELMLicense();
        } else {
            logToast("Need device admin before app can restrict functionality.", "info");
        }
    }
}

3) Store a boolean in SharedPreferences that saves whether the license has been activated before (you only have to activate ELM once).

public class LicenseReceiver extends BroadcastReceiver {

    private static final String TAG = "LicenseReceiver.java";

    private MainActivity mainActivity;

    public void setMainActivityHandler (MainActivity main) {
        mainActivity = main;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(EnterpriseLicenseManager.ACTION_LICENSE_STATUS)) {
            String status = intent.getStringExtra(EnterpriseLicenseManager.EXTRA_LICENSE_STATUS);
            int errorCode = intent.getIntExtra(EnterpriseLicenseManager.EXTRA_LICENSE_ERROR_CODE, 1);
            int extraResult = intent.getIntExtra(EnterpriseLicenseManager.EXTRA_LICENSE_RESULT_TYPE, 1);
            if (errorCode == 0) { // if successful, set license activation boolean flag to be true.
                mainActivity.setELMActivated();
            }
            mainActivity.logToast("ELM Status: " + status + " Error Code: " + errorCode, "info");
        }
    }
}

 

public void setELMActivated() {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("isELMActivated", true);
    editor.commit();
}

4) On startup, activate the license if it hasn't already been done so (can check through SharedPreferences boolean flag).

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ...

    sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    boolean isELMActivated = sharedPreferences.getBoolean("IsELMActivated", false);
    if (!isELMActivated) {
        activateELMLicense();
    }
}

Please let me know if you have any further questions.

Regards,

Samuel

Comments

Hi, Samuel. I am almost done with it. Just that it keep showing "Cannot resolve symbol 'sharedPreferences' " for the word sharedPreferences. May I know how can I solve it?

HON KEAT WONGAug 04, 2017 at 2:33 am
Samuel Veloso
Aug 04, 2017
4:39 pm

Hi,

You need to declare a SharedPreferences object in your Activity:

private SharedPreferences sharedPreferences;

Then, in your onCreate(), you need to initialize it:

sharedPreferences = getPreferences(Context.MODE_PRIVATE);

Hope it helps.

Regards,

Samuel