我正在我的应用程序中实现指纹身份验证。我已成功验证应用程序中的指纹。但问题是,我想从一个指纹帮助类调用Asynctask类,它位于主活动内部。
以下是FingerPrintHelper.java
类的代码:
public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {
private Context context;
// Constructor
public FingerprintHandler(Context mContext) {
context = mContext;
}
public void startAuth(FingerprintManager manager, FingerprintManager.CryptoObject cryptoObject) {
CancellationSignal cancellationSignal = new CancellationSignal();
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
return;
}
manager.authenticate(cryptoObject, cancellationSignal, 0, this, null);
}
@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
this.update("Fingerprint Authentication error\n" + errString, false);
}
@Override
public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
this.update("Fingerprint Authentication help\n" + helpString, false);
}
@Override
public void onAuthenticationFailed() {
this.update("Fingerprint Authentication failed.", false);
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
TextView lblFingerPrintError = (TextView) ((Activity)context).findViewById(R.id.lblFingerPrintError);
lblFingerPrintError.setVisibility(View.VISIBLE);
lblFingerPrintError.setText("Finger print did not match");
}
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
this.update("Fingerprint Authentication succeeded.", true);
context.startActivity(new Intent(context, HomePage.class));
}
public void update(String e, Boolean success){
if(success){
Log.i("WW", "Matched");
}
}
}
在方法onAuthenticationSucceeded()
中,我想调用主要活动的Asynctask类。
如果有人有解决方法,请回复。
谢谢。
答案 0 :(得分:1)
您可以将回调传递给您的通话活动,以了解身份验证完成情况,如下所示。
回调接口
public interface CallBackInterface {
void onAuthenticationSucceed();
}
当您从活动中调用 FingerprintHandler 时,只需使用方法或构造函数传递引用。
// Constructor
public FingerprintHandler(Context mContext,CallBackInterface callback) {
context = mContext;
this.callback = callback;
}
现在您可以使用此引用通知调用活动有关完成身份验证的操作,如下所示。
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
this.update("Fingerprint Authentication succeeded.", true);
//notify the caller about success
callback.onAuthenticationSucceed();
}
FingerprintHandler的最终代码如下。
public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {
private Context context;
private CallBackInterface callback;
// Constructor
public FingerprintHandler(Context mContext, CallBackInterface callback) {
context = mContext;
this.callback = callback;
}
public void startAuth(FingerprintManager manager, FingerprintManager.CryptoObject cryptoObject) {
CancellationSignal cancellationSignal = new CancellationSignal();
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
return;
}
manager.authenticate(cryptoObject, cancellationSignal, 0, this, null);
}
@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
this.update("Fingerprint Authentication error\n" + errString, false);
}
@Override
public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
this.update("Fingerprint Authentication help\n" + helpString, false);
}
@Override
public void onAuthenticationFailed() {
this.update("Fingerprint Authentication failed.", false);
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
TextView lblFingerPrintError = (TextView) ((Activity)context).findViewById(R.id.lblFingerPrintError);
lblFingerPrintError.setVisibility(View.VISIBLE);
lblFingerPrintError.setText("Finger print did not match");
}
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
this.update("Fingerprint Authentication succeeded.", true);
//here notify the caller about the success
callback.onAuthenticationSucceed();
// context.startActivity(new Intent(context, HomePage.class));
}
public void update(String e, Boolean success){
if(success){
Log.i("WW", "Matched");
}
}
}
在你使用构造函数传递引用的活动中,你必须覆盖 onAuthenticationSucceed(),所以现在在这里调用你的异步任务
@Override
public void onAuthenticationSucceed(){
//here start your async task.
}