简体   繁体   English

无法通过编程方式设置设备所有者

[英]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.我不能使用 ADB,因为我有超过 10K 的 android-9 无根设备。 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. process.waitFor() 总是返回 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.您可以为您的设备所有者 package 生成包含 URL 的 QR 码。 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.只需将信息替换为您自己的设备所有者 package 信息即可。 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.如果您成为 Google 的 EMM 合作伙伴,您甚至可以通过从制造商处获取预配置的 android 设备来进行零接触注册。 But Google stopped approving requests for custom device policy managers and you must use the Android Management API now.但 Google 已停止批准自定义设备策略管理器的请求,您现在必须使用 Android 管理 API。


Footnotes脚注

Kind of late answer but it may helps some developers in the future.有点晚的答案,但它可能会在未来帮助一些开发人员。 Below function works on Android 11.下面 function 适用于 Android 11。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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