方法无法在onStart()和onCreate()线程中循环

时间:2016-07-29 03:42:24

标签: java android multithreading android-asynctask android-fingerprint-api

我正在关注this教程。现在,我试图循环指纹认证部分,以便我可以保持重新认证用户指纹。我尝试在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);

        }
    }
}}

MainActivity

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

这是我的第一个个人项目,我对整个Android结构仍然相对较新。我非常感谢大家的任何意见。提前致谢

1 个答案:

答案 0 :(得分:0)

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