0

我正在关注教程。现在,我正在尝试循环指纹认证部分,以便我可以保持重新认证用户指纹。我尝试在 onStart() 和 onCreate() 中使用线程来循环身份验证,但应用程序在这两种情况下都卡住了。

只能验证一次的原始代码

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

    fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
    keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);

    if (!keyguardManager.isKeyguardSecure()){
        Toast.makeText(this,
                "Lock screen security is not enable in Settings", Toast.LENGTH_LONG).show();
        return;
    }

    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED){
        Toast.makeText(this,
                "Fingerprint authentication permission is not enabled", Toast.LENGTH_LONG).show();
        return;
    }

    if (!fingerprintManager.hasEnrolledFingerprints()){
        Toast.makeText(this, "Register at least one fingerprint in Settings", Toast.LENGTH_LONG).show();
        return;
    }

    generateKey();
    if (cipherInit()){
        cryptoObject = new FingerprintManager.CryptoObject(cipher);
        FingerprintHandler helper = new FingerprintHandler(this);
        helper.startAuth(fingerprintManager, cryptoObject);

    }

}

onStart() / onCreate() 中的线程失败

    @Override
    protected void onStart() {
        super.onStart();
        new Thread(new Runnable(){
        public void run() {
            while(true)
                {
                    try {
                    Thread.sleep(50);
                    if (cipherInit()) {
                         cryptoObject = new FingerprintManager.CryptoObject(cipher);
                         FingerprintHandler helper = new FingerprintHandler(MainActivity.this);
                         helper.startAuth(fingerprintManager, cryptoObject);

                 }} catch (InterruptedException e){
                         e.printStackTrace();
            }
        }
    }
}).start();}

除了使用线程之外,我还尝试使用 AsyncTask 为我执行 while 循环。这是我创建课程的尝试。我的问题是 cipherInit() 驻留在 MainActivity.java 中,如何从我的 Looping 类调用该方法?

循环.java

    import android.hardware.fingerprint.FingerprintManager;
    import android.os.AsyncTask;

    public class Looping extends AsyncTask<Object,Void,Void> {
        FingerprintManager fingerprintManager;
        FingerprintManager.CryptoObject cryptoObject;
        Cipher cipher;
        @Override
        protected Void doInBackground(Void... arg0) {
            cipher = (Cipher) arg0[0];
            while(true) {
                if (cipherInit()) {
                    cryptoObject = new FingerprintManager.CryptoObject(cipher);
                    FingerprintHandler helper = new FingerprintHandler(MainActivity.this);
                    helper.startAuth(fingerprintManager, cryptoObject);

        }
    }
}}

主要活动

            Looping loop = new Looping();
            loop.execute(cipher, null, null);

这是我的第一个个人项目,我对整个 Android 结构还比较陌生。我将非常感谢大家的任何意见。提前致谢

4

1 回答 1

0

您不需要辅助线程或循环来进行身份验证。调用FingerprintManager.authenticate()是在您的FingerprintHandler(假设您具有与您引用的教程相同的代码)中完成的。这是一个 asyc 操作,并且FingerprintManager.AuthentciationCallback在身份验证成功或失败时回调处理程序 ( )。您需要根据该成功/失败采取行动,而不是while循环轮询。该回调将发生在您的主线程上。

于 2016-07-29T11:38:56.937 回答