4

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 ?

Mervin Hemaraju
  • 1,921
  • 2
  • 22
  • 71

1 Answers1

0

Acording to Android documentation, you need either admin previlages or LOCK_DEVICE permission.

enter image description here

With admin previlages, as mentioned the unlock will have to be done using pattern or pin.

LOCK_DEVICE permission is not available for 3rd party apps.

enter image description here

Looks like there is an option to acheive this if you are building a kiosk app. I havent explored more on this.

enter image description here Hope this helps.

Don Peter
  • 103
  • 1
  • 7