Use screen lock in my app

夙愿已清 提交于 2020-04-07 18:14:38

问题


Is it possible to use the default security settings, which user has set to the phone, as a locking or login mechanism for my app too? I mean like when we reset the phone, it asks for phone password or pattern.

Is it possible the same way that I can use the default android password or pattern set by user to login to my app?

My goal is to bypass the developing effort and use some standard way of authentication without making user to remember another new password.

NOTE: I'm aware that I can lock the screen programmatically. But instead, I want to use the lock as a verification before performing any critical operation. (Just like how Settings ask for the password before resetting the the phone.)


回答1:


Actually, there is an API to exactly that using the KeyguardManager.

First get a the Keyguard SystemService:

KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);

And then request an authentication intent using:

Intent i = km.createConfirmDeviceCredentialIntent(title,description);

start this intent using startActivityForResult(Intent, int) and check for RESULT_OK if the user successfully completes the challenge.

This is for API level 21. Previous versions might work with KeyguardLock.




回答2:


I'm just following @agi with few enhancement,

    public class MainActivity extends AppCompatActivity {
    private static int CODE_AUTHENTICATION_VERIFICATION=241;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
        if(km.isKeyguardSecure()) {

            Intent i = km.createConfirmDeviceCredentialIntent("Authentication required", "password");
            startActivityForResult(i, CODE_AUTHENTICATION_VERIFICATION);
        }
        else
            Toast.makeText(this, "No any security setup done by user(pattern or password or pin or fingerprint", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK && requestCode==CODE_AUTHENTICATION_VERIFICATION)
        {
            Toast.makeText(this, "Success: Verified user's identity", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(this, "Failure: Unable to verify user's identity", Toast.LENGTH_SHORT).show();
        }
    }
}


来源:https://stackoverflow.com/questions/29456456/use-screen-lock-in-my-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!