简体   繁体   English

如何将device_owner设置为我的Android应用程序?

[英]How can I set device_owner to my android app?

I tried to set my application the device_owner of my tablet (without ROOT or NFC), with the command: 我尝试使用以下命令将我的应用程序设置为平板电脑的device_owner(没有ROOT或NFC):

adb shell dpm set-device-owner com.test.my_device_owner_app/.MyDeviceAdminReceiver

like is written on many sites (because i have to make a KIOSK APP). 喜欢写在很多网站上(因为我必须制作一个KIOSK APP)。 First of all i made a factory reset, then i installed my app, and then i wrote this command on shell, but the answer is: 首先我重置工厂,然后我安装了我的应用程序,然后我在shell上写了这个命令,但答案是:

java.lang.IllegalStateException: Trying to set device owner but device is already provisioned. java.lang.IllegalStateException:尝试设置设备所有者但已设置设备。
at android.os.Parcel.readException(Parcel.java:1554) 在android.os.Parcel.readException(Parcel.java:1554)
at android.os.Parcel.readException(Parcel.java:1499) 在android.os.Parcel.readException(Parcel.java:1499)
at android.app.admin.IDevicePolicyManager$Stub$Proxy.setDeviceOwner(IDevicePolicyManager.java:3212) 在android.app.admin.IDevicePolicyManager $ Stub $ Proxy.setDeviceOwner(IDevicePolicyManager.java:3212)
at com.android.commands.dpm.Dpm.runSetDeviceOwner(Dpm.java:114) 在com.android.commands.dpm.Dpm.runSetDeviceOwner(Dpm.java:114)
at com.android.commands.dpm.Dpm.onRun(Dpm.java:82) 在com.android.commands.dpm.Dpm.onRun(Dpm.java:82)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:47) 在com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
at com.android.commands.dpm.Dpm.main(Dpm.java:38) 在com.android.commands.dpm.Dpm.main(Dpm.java:38)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method) at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:249) 在com.android.internal.os.RuntimeInit.main(RuntimeInit.java:249)

Now, how can i resolve this problem without rooting the tablet? 现在,如何在不支持平板电脑的情况下解决此问题?

I've got the same problem with Lenovo Yoga 2 Tablet. 联想Yoga 2平板电脑也遇到了同样的问题。

Here is some snipped of the dpm source that I found while researching this problem. 这里有一些我在研究这个问题时发现的dpm源代码。

    if (!allowedToSetDeviceOwnerOnDevice()) {
        throw new IllegalStateException(
                "Trying to set device owner but device is already provisioned.");
    }

    if (mDeviceOwner != null && mDeviceOwner.hasDeviceOwner()) {
        throw new IllegalStateException(
                "Trying to set device owner but device owner is already set.");
    }

And here is the allowedToSetDeviceOwnerOnDevice implementation 这是allowedToSetDeviceOwnerOnDevice实现

/**
 * 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.
 */
private boolean allowedToSetDeviceOwnerOnDevice() {
    int callingId = Binder.getCallingUid();
    if (callingId == Process.SHELL_UID || callingId == Process.ROOT_UID) {
        return AccountManager.get(mContext).getAccounts().length == 0;
    } else {
        return Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.DEVICE_PROVISIONED, 0) == 0;
    }
}

So first make sure that all accounts are removed. 因此,首先要确保删除所有帐户。 Check Settings > Accounts. 检查设置>帐户。 Lenovos bloat ware has created the Local Calendar account by default. Lenovos膨胀软件默认创建了本地日历帐户。 You have to remove it. 你必须删除它。

For those who have root access 对于那些有root权限的人

See the SO answer for manually creating the device_owner.xml . 请参阅SO答案以手动创建device_owner.xml As I can see from the implementation, dpm is doing the same thing as described in the answer. 正如我从实现中看到的那样,dpm正在做与答案中描述的相同的事情。 By the way, I leave the name attribute with no problems. 顺便说一句,我保留name属性没有问题。

When you look in the else case, you can bypass the test by calling 当您查看else情况时,您可以通过调用绕过测试

settings put global device_provisioned 0

My experience with Yoga tablet 我对瑜伽平板电脑的体验

Even I have root access, doing factory reset and trying the device_owner.xml method I haven't success yesterday. 即使我有root访问权限,进行工厂重置并尝试使用device_owner.xml方法,我昨天也没有成功。

What I've done today was to login with a google account (yesterday I've skipped this part) and after remove also this account in Settings > Accounts I was able (as su ) to run the dpm command successfully. 我今天所做的是使用谷歌帐户登录(昨天我已跳过此部分),并在删除此设置>帐户中的帐户后我能够(作为su )成功运行dpm命令。

Update 更新

I've got another Yoga 2 tablet without root access and without login with my google account and have successfully set the device owner. 我有另一台没有root访问权限的Yoga 2平板电脑,没有使用我的谷歌帐户登录,并成功设置了设备所有者。

One think I can recommend you: Try to close Android Studio after installing your kiosk mode app. 有人认为我可以推荐你:在安装自助服务终端模式应用程序后尝试关闭Android Studio。 Maybe this causes another Binder.getCallingUid() . 也许这会导致另一个Binder.getCallingUid()

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

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