The solution is to use: Device Administration API
The device administration API is specially designed for enterprise Applications. And this API provides administration features at the system level.
so how does it work?
You use the Device Administration API to write device admin applications that users install on their devices. The device admin application enforces the desired policies. Here's how it works:
If users do not enable the device admin app, it remains on the device, but in an inactive state. Users will not be subject to its policies, and they will conversely not get any of the application's benefits—for example, they may not be able to sync data.
If a user fails to comply with the policies (for example, if a user sets a password that violates the guidelines), it is up to the application to decide how to handle this. However, typically this will result in the user not being able to sync data.
If a device attempts to connect to a server that requires policies not supported in the Device Administration API, the connection will not be allowed. The Device Administration API does not currently allow partial provisioning. In other words, if a device (for example, a legacy device) does not support all of the stated policies, there is no way to allow the device to connect.
If a device contains multiple enabled admin applications, the strictest policy is enforced. There is no way to target a particular admin application.
To uninstall an existing device admin application, users need to first unregister the application as an administrator.
Here is a sample code that implements the same:
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
public class ActiveDevicePolicy extends Activity {
private DevicePolicyManager activeDevicePolicyManager;
private final String LOG_TAG = "ActiveDevicePolicy";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_active_device_policy);
activeDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
List<ComponentName> activeAdmins = activeDevicePolicyManager.getActiveAdmins();
if(activeAdmins != null && !activeAdmins.isEmpty()){
for(int index = 0; index < activeAdmins.size(); index++ ){
Log.i(LOG_TAG, "flattenToShortString: "+ activeAdmins.get(index).flattenToShortString());
Log.i(LOG_TAG, "flattenToString: "+ activeAdmins.get(index).flattenToString());
Log.i(LOG_TAG, "getClassName: "+ activeAdmins.get(index).getClassName());
Log.i(LOG_TAG, "getPackageName: "+ activeAdmins.get(index).getPackageName());
Log.i(LOG_TAG, "getShortClassName: "+ activeAdmins.get(index).getShortClassName());
Log.i(LOG_TAG, "toShortString: "+ activeAdmins.get(index).toShortString());
}
} else {
Log.i(LOG_TAG, "No Active Device Policy Manager");
}
}
}
In the above code I have called DevicePolicyManager.getActiveAdmins()
method to retrieve the list of all the admins that are active at the moment. Now the interesting part here is that, since its returning a list; there could be more than one device admins in that list. So the strictest policy remains active!
Hope it helps!