【问题标题】:Method failed to loop in onStart() and onCreate() thread方法在 onStart() 和 onCreate() 线程中循环失败
【发布时间】:2016-07-29 03:42:23
【问题描述】:

我正在关注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 类调用该方法?

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 结构还比较陌生。我将非常感谢大家的任何意见。提前致谢

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 您是说我应该在onAuthenticationSucceeded()onAuthenticationFailed() 中的FingerprintHandler 中“继续”或“循环”或“重新继续”我的身份验证吗?我怎样才能继续并重新调用整个事情或处理回调?我对这个异步回调不太熟悉。
    • 您可以根据调用的回调方法确定要采取的操作。但它是一个异步操作,你不应该只是循环等待某事。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 2019-05-26
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多