简体   繁体   English

我无法在设备上为我的应用设置设备所有者

[英]I cannot set device owner for my app on device

I have been working on setting my apps to device owner through adb shell dpm command but the error came out我一直在努力通过 adb shell dpm 命令将我的应用程序设置为设备所有者,但错误出现了

 Error: Bad admin: ComponentInfo{com.example.oshao.autolock/com.example.oshao.

autolock.AdminReceiver} autolock.AdminReceiver}

Here is my activity and manifests这是我的活动和清单

public class MainActivity extends AppCompatActivity {

private DevicePolicyManager mDpm;
private boolean isKioskModeEnabled = false;
private Button btnEnabled;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnEnabled = (Button) findViewById(R.id.btnEnable);
    btnEnabled.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            enableKioskMode(!isKioskModeEnabled);
        }
    });

    ComponentName deviceAdmin = new ComponentName(this, AdminReceiver.class);
    mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    if (!mDpm.isAdminActive(deviceAdmin)) {
        Toast.makeText(this, "This ap is not a device admin", Toast.LENGTH_SHORT).show();
    }

    if (mDpm.isDeviceOwnerApp(getPackageName())) {
        mDpm.setLockTaskPackages(deviceAdmin, new String[]{getPackageName()});
    } else {
        Toast.makeText(this, "This app is not the device owner", Toast.LENGTH_SHORT).show();
    }
}

private void enableKioskMode(boolean enabled) {

    if (enabled) {

        if (mDpm.isLockTaskPermitted(this.getPackageName())) {

            startLockTask();
            isKioskModeEnabled = true;
            btnEnabled.setText("Exit Kiosk Mode");

        } else {
            Toast.makeText(this, "Kiosk Mode is not permitted", Toast.LENGTH_SHORT).show();
        }
    } else {

        stopLockTask();
        isKioskModeEnabled = false;
        btnEnabled.setText("Enter Kiosk Mode");

    }

}
}

the manifest:清单:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">


    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name="com.example.oshao.autolock.AdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN">

        <meta-data
            android:name="android.app.admin"
            android:resource="@xml/device_admin"/>
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>

    </receiver>

</application>

I am not sure why the error is still out there, is that because one device could only have single app that be set device owner?我不确定为什么错误仍然存​​在,是因为一台设备只能有一个被设置为设备所有者的应用程序吗? Another question is that the adb command could be implemented on a device without account and connect to pc in order to input command in terminal.另一个问题是adb命令可以在没有账号的设备上执行,连接到pc以便在终端输入命令。 Can I do it by code in app under the circumstances the device is not rooted because I have several devices and it is hard to set them one by one, Thanks在设备没有root的情况下,我可以通过应用程序中的代码来完成,因为我有多个设备并且很难一一设置它们,谢谢

I solved the bad admin error when I followed this tutorial: http://www.sureshjoshi.com/mobile/android-kiosk-mode-without-root/我按照本教程解决了错误的管理错误: http : //www.sureshjoshi.com/mobile/android-kiosk-mode-without-root/

I was missing the src/main/res/xml/device_admin.xml file, which only contained:我错过了src/main/res/xml/device_admin.xml文件,它只包含:

<device-admin> </device-admin>

您必须在设置设备所有者之后首先设置活动管理员,并注意您的包名称必须正确。

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

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