class MyCallBack extends FingerprintManager.AuthenticationCallback {
boolean mSelfCancelled;
private FingerprintManager fingerPrintManager;
private CancellationSignal mCancellationSignal;
TextView prompt;
Activity mActivity;
public MyCallBack(FingerprintManager mFingerPrintManager, TextView prompt, Activity mActivity) {
this.fingerPrintManager = mFingerPrintManager;
this.prompt = prompt;
this.mActivity = mActivity;
}
public boolean isFingerprintAuthAvailable() {
return fingerPrintManager.isHardwareDetected()
&& fingerPrintManager.hasEnrolledFingerprints();
}
public void startListening(FingerprintManager.CryptoObject cryptoObject) {
if (!isFingerprintAuthAvailable()) {
return;
}
mCancellationSignal = new CancellationSignal();
mSelfCancelled = false;
prompt.setText("");
fingerPrintManager
.authenticate(cryptoObject, mCancellationSignal, 0 /* flags */, this, null);
}
public void stopListening() {
if (mCancellationSignal != null) {
mSelfCancelled = true;
mCancellationSignal.cancel();
mCancellationSignal = null;
}
}
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
LogUtil.d(TAG, "onAuthenticationError");
prompt.setText("" + errString);
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
LogUtil.d(TAG, "onAuthenticationFailed");
prompt.setText("onAuthenticationFailed");
}
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
LogUtil.d(TAG, "onAuthenticationSucceeded");
prompt.setText("");
if (getActivity() instanceof Login2Activity) {
((Login2Activity)getActivity()).goToMain();
}
getActivity().finish();
}
@Override
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
super.onAuthenticationHelp(helpCode, helpString);
prompt.setText("" + helpString);
}
}
调用;
public void onResume() {
super.onResume();
if (myCallBack != null) {
myCallBack.startListening(null);
}
((AnimationDrawable)icon.getDrawable()).start();
}
@Override
public void onPause() {
super.onPause();
if (myCallBack != null) {
myCallBack.stopListening();
}
((AnimationDrawable)icon.getDrawable()).stop();
}