'I I Want Build App For: Ask For Pattern(KeyguardManager) Even App Is Not Running In Background For If Someone Tries To Shutdown Android Phone

I Want To Make An Android App That Asks For Pattern Or Password If Android Is Tried To Shutdown Or PowerOff When The Mobile Is Lock. I Saw This Feature In The Samsung Edge S6 Phone. If The Phone Is Locked And If I Tries To Shutdown Or Turn On Off WiFi Or Bluetooth Then It Asks For Entering A Password.

Here Is The Complete Source Code Of App: Source Code

So I Want To Implement This Feature In My App If Is Locked Then App Should Be Asked For Pattern For PowerOff & Turning Off WiFi. Source Code

I've Tried This With BooTComplete

KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);

    if(km.isKeyguardSecure()) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Intent i = km.createConfirmDeviceCredentialIntent("Authentication required", "password");
            startActivityForResult(i, CODE_AUTHENTICATION_VERIFICATION);
        }

    }
    else {
        Toast.makeText(this, "No Any Password Or Pattern Set Please Set First", Toast.LENGTH_SHORT).show();
    }

For Background Running App I've Tried This Code That Mentioned Below In My App But Doesn't Work For Background Running App

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

        registerActivityLifecycleCallbacks((Application.ActivityLifecycleCallbacks) this);
    }

Here Is The Complete Source Code Of App: Source Code



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source