我正在关注本教程。现在,我正在尝试循环指纹认证部分,以便我可以保持重新认证用户指纹。我尝试在 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 结构还比较陌生。我将非常感谢大家的任何意见。提前致谢