简体   繁体   中英

Unable to set device owner through programmatically

I tried to execute the following code to set my app as a device owner. I can't use ADB because I have more than 10K android-9 non rooted devices. So, Need to do this programmatically.

   String name = AdminReceiver.class.getName();
   if (name.startsWith(BuildConfig.APPLICATION_ID)) {
           name = name.substring(BuildConfig.APPLICATION_ID.length());
   }
   final String command = "dpm set-device-owner " + BuildConfig.APPLICATION_ID + '/' + name;
   Process process = Runtime.getRuntime().exec(command);
   Log.d(TAG,"RETURN VALUE:"process.waitFor();

process.waitFor() always return 1.

Need to do this programmatically

Fortunately, that is not possible, for blindingly obvious security reasons.

Use QR Enrollment instead!

You can generate a QR Code which contains a URL for your Device Owner package. Then you can scan this code in the factory setup process of the device.

See the documentation here

The Data in the QR Code would look something like this:

{
    "android.app.extra.PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME": "com.google.android.apps.work.clouddpc/.receivers.CloudDeviceAdminReceiver",
    "android.app.extra.PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM": "I5YvS0O5hXY46mb01BlRjq4oJJGs2kuUcHvVkAPEXlg",
    "android.app.extra.PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION": "https://play.google.com/managed/downloadManagingApp?identifier=setup"
}

Simply replace the information with your own device owner package information. After scanning, the device will download, install, and set up the device owner.


If you become an EMM Partner with Google, you can even do Zero-Touch-Enrollment by getting preconfigured android devices from the manufacturers. But Google stopped approving requests for custom device policy managers and you must use the Android Management API now.


Footnotes

Kind of late answer but it may helps some developers in the future. Below function works on Android 11.

try {
Runtime.getRuntime().exec("dpm set-device-owner --user 0 
com.example.myapp/com.example.myapp.DeviceAdminRcvr");
} catch (Exception e) {
e.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM