试图在模拟器中让我的应用程序设备所有者

Trying to make my application device owner in simulator

我正在尝试做什么:我正在尝试让应用程序成为设备所有者

我在用什么:我在用模拟器试试这个


清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cnx.silentupdate">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

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

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver
            android:name=".DevAdminReceiver"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>

            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_admin" />
        </receiver>

        <receiver
            android:name=".UpdateReceiver">

            <intent-filter >
                <action  android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
            </intent-filter>
        </receiver>


    </application>

</manifest>

DevAdminReceiver.kt

class DevAdminReceiver : DeviceAdminReceiver() {
    override fun onEnabled(context: Context?, intent: Intent?) {
        super.onEnabled(context, intent)
        Log.d(TAG, "Device Owner Enabled")
        Toast.makeText(context,"Device Owner Enabled",Toast.LENGTH_LONG).show()
    }
}

UpdateReceiver.kt

class UpdateReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {

        // Restart app here
        val i = Intent(context, MainActivity::class.java)
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(i)
        Toast.makeText(context,"Restart app here", Toast.LENGTH_LONG).show()
    }
}

device_admin.xml

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
    </uses-policies>
</device-admin>

MainActivity.kt

 @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    override fun onResume() {
        super.onResume()
        val dpm = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
        if (dpm.isDeviceOwnerApp(packageName)){
            Log.d(TAG, "is device owner")
            Toast.makeText(this,"is device owner", Toast.LENGTH_LONG).show()
        }else{
            Log.d(TAG, "not device owner")
            Toast.makeText(this,"not device owner", Toast.LENGTH_LONG).show()
        }

        tv_text.text = "version code=" + BuildConfig.VERSION_CODE
    }

当我尝试命令时: 我得到如下错误

admins-MacBook-Pro:platform-tools devrath$ ./adb shell dpm set-device-owner eu.sisik.devowner/.DevAdminReceiver
    usage: dpm [subcommand] [options]
    usage: dpm set-active-admin [ --user <USER_ID> | current ] <COMPONENT>
    usage: dpm set-device-owner [ --user <USER_ID> | current *EXPERIMENTAL* ] [ --name <NAME> ] <COMPONENT>
    usage: dpm set-profile-owner [ --user <USER_ID> | current ] [ --name <NAME> ] <COMPONENT>
    usage: dpm remove-active-admin [ --user <USER_ID> | current ] [ --name <NAME> ] <COMPONENT>

    dpm set-active-admin: Sets the given component as active admin for an existing user.

    dpm set-device-owner: Sets the given component as active admin, and its package as device owner.

    dpm set-profile-owner: Sets the given component as active admin and profile owner for an existing user.

    dpm remove-active-admin: Disables an active admin, the admin must have declared android:testOnly in the application in its manifest. This will also remove device and profile owners.

    dpm clear-freeze-period-record: clears framework-maintained record of past freeze periods that the device went through. For use during feature development to prevent triggering restriction on setting freeze periods.

    dpm force-security-logs: makes all security logs available to the DPC and triggers DeviceAdminReceiver.onSecurityLogsAvailable() if needed.

    Error: Unknown admin: ComponentInfo{eu.sisik.devowner/eu.sisik.devowner.DevAdminReceiver}

I figured out the problem: I had given the package name wrong

正确的格式是:

admins-MacBook-Pro:platform-tools devrath$ ./adb shell dpm set-device-owner com.cnx.silentupdate/.DevAdminReceiver