android - 如何从 FIngerPrintHelper class 在 Main Activity 中调用 class 扩展 Asynctask
android - How to call a class extending Asynctask in the Main Activity from FIngerPrintHelper class
我正在我的应用程序中实施指纹验证。我已在应用程序中成功验证指纹。但问题是,我想从指纹助手 class 调用 Main Activity 中的 Asynctask class。
下面是 FingerPrintHelper.java
class 的代码:
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 Class,它是 Main Activity。
如果有人对此有解决方案,请回复。
谢谢。
您可以将回调传递给您的调用 activity 以了解身份验证完成,如下所示。
回调接口
public interface CallBackInterface {
void onAuthenticationSucceed();
}
当您从 activity 调用 FingerprintHandler 时,只需使用这样的方法或构造函数传递引用。
// Constructor
public FingerprintHandler(Context mContext,CallBackInterface callback) {
context = mContext;
this.callback = callback;
}
现在您可以使用此引用通知调用方 activity 身份验证完成,如下所示。
@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");
}
}
}
并且在您使用构造函数传递引用的 activity 中,您必须覆盖 onAuthenticationSucceed() 所以现在在这里调用您的异步任务
@Override
public void onAuthenticationSucceed(){
//here start your async task.
}
我正在我的应用程序中实施指纹验证。我已在应用程序中成功验证指纹。但问题是,我想从指纹助手 class 调用 Main Activity 中的 Asynctask class。
下面是 FingerPrintHelper.java
class 的代码:
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 Class,它是 Main Activity。
如果有人对此有解决方案,请回复。
谢谢。
您可以将回调传递给您的调用 activity 以了解身份验证完成,如下所示。
回调接口
public interface CallBackInterface {
void onAuthenticationSucceed();
}
当您从 activity 调用 FingerprintHandler 时,只需使用这样的方法或构造函数传递引用。
// Constructor
public FingerprintHandler(Context mContext,CallBackInterface callback) {
context = mContext;
this.callback = callback;
}
现在您可以使用此引用通知调用方 activity 身份验证完成,如下所示。
@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");
}
}
}
并且在您使用构造函数传递引用的 activity 中,您必须覆盖 onAuthenticationSucceed() 所以现在在这里调用您的异步任务
@Override
public void onAuthenticationSucceed(){
//here start your async task.
}