4

I have an android app which is installed on 100+ devices. (Android 5.1.1 API22 and 6.0.1 API 23)

https://developer.android.com/reference/android/app/admin/package-summary.html

I went through all these references but no luck.

Using the devicePolicyManager, I get the error: XXXXX App is not the device owner.

I know there is a way to get device owner by shell command (ADB), but I can't do that on all the devices individually via usb.

DevicePolicyManager deviceManger = (DevicePolicyManager)Forms.Context.GetSystemService(Context.DevicePolicyService); ComponentName demoDeviceAdmin = new ComponentName(Forms.Context, Java.Lang.Class.FromType(typeof(DeviceAdmin))); deviceManger.SetGlobalSetting(demoDeviceAdmin, "wifi_device_owner_configs_lockdown", "1");

peterh
  • 11,875
  • 18
  • 85
  • 108
Guest
  • 265
  • 2
  • 5
  • 13

3 Answers3

6

The source code says, 'Device owner can only be set on an unprovisioned device, unless it was initiated by “adb”, in which case we allow it if no account is associated with the device'

If you don't have any accounts set up, you can set it programmatically using dpm:

try {
    Runtime.getRuntime().exec("dpm set-device-owner com.example.deviceowner/.MyDeviceAdminReceiver");
} catch (Exception e) {
    Log.e(TAG, "device owner not set");
    Log.e(TAG, e.toString());
    e.printStackTrace();
}

Reference: http://florent-dupont.blogspot.fr/2015/01/android-shell-command-dpm-device-policy.html

Steve Miskovetz
  • 2,360
  • 13
  • 29
  • 6
    I'm executing same statement, but returns null and not applied anything...what can be ? – Dhruv Patel Dec 16 '17 at 12:00
  • @Dhruv Hello, I've been experiencing the exact same thing. I know it's been awhile, but any clue on how you resolved this issue? – Eugenio Lopez Oct 06 '18 at 19:31
  • 3
    I've tried implementing this and have found that it only works on rooted devices. otherwise dpm command can only be used within the adb shell. If theres another way to execute this command programmatically w/o root then please share. – Eugenio Lopez Oct 12 '18 at 15:07
2

There are a few different ways you get take device owner of an Android Device. Depending on if the devices are owned by you or its a BYOD, you can use different methods. This table by Google summarizes all the possible ways you can take device ownership. You can find it here.

kash
  • 830
  • 2
  • 7
  • 19
-1

i have created a method MakeOwner() and called in the onCreate method my by lucky it worked well...

 public void MakeOwner(){
      try {
            Runtime.getRuntime().exec("dpm set-device-owner com.exampledemo.parsaniahardik.scanbarcodeqrdemonuts/.BasicDeviceAdminReceiver");
        } catch (Exception e) {
            Log.e(TAG, "device owner not set");
            Log.e(TAG, e.toString());
            e.printStackTrace();
        }
    }
  • Hello, I'm executing the same statement, but returns null and nothing is applied. Since I'm on a windows, i've tried prefixing cmd, but exception is thrown(Error running exec(). Command: [cmd, set-device-owner] Working Directory: null Environment: null ). Any help would be greatly appreciated. – Eugenio Lopez Oct 05 '18 at 18:02