I am trying to create an app that, upon launching, will lock the phone. (It acts as an alternative of pressing the physical lock button on the phone instead).
I was able to achieve it with this code:
MainActivity.kt
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
LockmeTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Android")
}
}
}
val deviceManger =
getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
val compName = ComponentName(this, AdminReceiver::class.java)
val active: Boolean = deviceManger.isAdminActive(compName)
if (!active) {
val intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, compName)
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "You should enable the app!")
startActivity(intent)
}else{
deviceManger.lockNow()
}
}
}
policies.xml
<?xml version="1.0" encoding="utf-8"?>
<device-admin>
<uses-policies>
<force-lock />
</uses-policies>
</device-admin>
AndroidManifest.xml
<receiver
android:name= ".AdminReceiver"
android:description= "@string/app_name"
android:label= "@string/app_name"
android:exported="false"
android:permission= "android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name= "android.app.device_admin"
android:resource= "@xml/policies" />
<intent-filter>
<action android:name= "android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
The issue here is that, after locking the phone, when I try to unlock it, it says Device is locked by admin
and it needs strong authentication such as Pin / Password to unlock. I want it to use the non-strong authentication that was setup on the phone such as fingerprint or face unlock.
Any idea how i can achieve this ?