将Android设备设置为Android Enterprise,并将其与Google Workspace EnterpriseID相关联,以实现设备管理和安全性。
示例代码:
//检查设备是否支持Android企业版 PackageManager pm = getPackageManager(); boolean isDeviceSupportsAndroidForWork = pm.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN);
//创建Android设备管理员 DevicePolicyManager mDevicePolicyManager = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName mAdminComponentName = new ComponentName(MainActivity.this, DeviceAdminReceiver.class);
//检查是否为设备管理员 boolean isActiveDeviceAdmin = mDevicePolicyManager.isAdminActive(mAdminComponentName);
//检查设备是否已启用Android企业版 boolean isAndroidForWorkEnabled = mDevicePolicyManager.isProfileOwnerApp(getPackageName());
//启用Android企业版 if (isDeviceSupportsAndroidForWork && !isAndroidForWorkEnabled) { //设置Google Workspace EnterpriseID final String enterpriseId = "enterprise_id"; Intent intent = new Intent(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE); intent.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, getPackageName()); intent.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, mAdminComponentName); intent.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, bundleExtras(enterpriseId)); startActivityForResult(intent, CODE_ENABLE_ADMIN); }
private Bundle bundleExtras(String enterpriseId) { Bundle extras = new Bundle(); if (enterpriseId != null && !enterpriseId.isEmpty()) { extras.putString(DevicePolicyManager.EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED, enterpriseId); } return extras; }