class AdminReceiver: DeviceAdminReceiver()
<receiver
android:name=".AdminReceiver"
android:label="@string/app_name"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
</uses-policies>
</device-admin>
<activity
android:name=".FirstActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
adb shell
dpm set-device-owner com.your.package_name/.AdminReceiver
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val myDevicePolicyManager = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
val mDPM = ComponentName(this, AdminReceiver::class.java)
if (myDevicePolicyManager.isDeviceOwnerApp(this.packageName)) {
val packages = arrayOf(this.packageName)
myDevicePolicyManager.setLockTaskPackages(mDPM, packages)
startLockTask()
} else {
Toast.makeText(applicationContext, "Not device owner", Toast.LENGTH_LONG).show()
}
}