Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Device Admin API, how to be a device owner?

Tags:

android

I want to use DevicePolicyManager method: setSecureSetting. This method need profil or device owner:

Called by profile or device owners to update Settings.Secure settings.

My application has activated Device Admin because when I called isAdminActive it returns true.

But when I called setSecureSetting I got exception:

E/DevicePolicyUtility( 9901): java.lang.SecurityException: Admin ComponentInfo{com.xxxx/com.xxxx.MyDeviceAdminReceiver} does not own the profile
E/DevicePolicyUtility( 9901):   at android.os.Parcel.readException(Parcel.java:1546)
E/DevicePolicyUtility( 9901):   at android.os.Parcel.readException(Parcel.java:1499)
E/DevicePolicyUtility( 9901):   at android.app.admin.IDevicePolicyManager$Stub$Proxy.setSecureSetting(IDevicePolicyManager.java:4300)
E/DevicePolicyUtility( 9901):   at android.app.admin.DevicePolicyManager.setSecureSetting(DevicePolicyManager.java:3399)

So, how to be a device owner on API ? I don't find in http://developer.android.com/guide/topics/admin/device-admin.html or http://developer.android.com/reference/android/app/admin/DevicePolicyManager.html


I try this on API Level 23:

Intent intent = new Intent(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE);
intent.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,ctx.getPackageName());
if (intent.resolveActivity(ctx.getPackageManager()) != null) {
    ctx.startActivity(intent);
} else {
    Toast.makeText(ctx, "Stopping.", Toast.LENGTH_SHORT).show();
}

And I got message:enter image description here

like image 521
LaurentY Avatar asked Sep 24 '15 14:09

LaurentY


1 Answers

To become a device owner, there's 2 possibilities

  • by NfcProvisioning as explain in this sample: https://developer.android.com/samples/NfcProvisioning/index.html
  • Or by command line, with dpm tool as this:

    adb shell

    dpm set-device-owner com.mycompany.deviceowner/.DeviceAdminReceiver

dpm set-device-owner: Sets the given component as active admin, and its package as device owner.

Thanks to Florent Dupont: http://florent-dupont.blogspot.fr/2015/01/android-shell-command-dpm-device-policy.html

like image 80
LaurentY Avatar answered Sep 17 '22 20:09

LaurentY