标签:over oct sig 停止 hand port 引入 系统调用 function
public class FingerprintUtil{
private FingerprintManagerCompat mFingerprintManager;
private KeyguardManager mKeyManager;
private CancellationSignal mCancellationSignal;
private Activity mActivity;
public FingerprintUtil(Context ctx) {
mActivity = (Activity) ctx;
mFingerprintManager = FingerprintManagerCompat.from(mActivity);
mKeyManager = (KeyguardManager) mActivity.getSystemService(Context.KEYGUARD_SERVICE);
}
public void callFingerPrintVerify(final IFingerprintResultListener listener) {
if (!isHardwareDetected()) {
return;
}
if (!isHasEnrolledFingerprints()) {
if (listener != null) {
listener.onNoEnroll();
}
return;
}
if (!isKeyguardSecure()) {
if (listener != null) {
listener.onInSecurity();
}
return;
}
if (listener != null) {
listener.onSupport();
}
if (listener != null) {
listener.onAuthenticateStart();
}
if (mCancellationSignal == null) {
mCancellationSignal = new CancellationSignal();
}
try {
mFingerprintManager.authenticate(null, 0, mCancellationSignal, new FingerprintManagerCompat.AuthenticationCallback() {
//多次尝试都失败会走onAuthenticationError。会停止响应一段时间。提示尝试次数过多。请稍后再试。
@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
if (listener != null)
listener.onAuthenticateError(errMsgId, errString);
}
//指纹验证失败走此方法,比如小米前4次验证失败走onAuthenticationFailed,第5次走onAuthenticationError
@Override
public void onAuthenticationFailed() {
if (listener != null)
listener.onAuthenticateFailed();
}
@Override
public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
if (listener != null)
listener.onAuthenticateHelp(helpMsgId, helpString);
}
//当验证的指纹成功时会回调此函数。然后不再监听指纹sensor
@Override
public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
if (listener != null)
listener.onAuthenticateSucceeded(result);
}
}, null);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 是否录入指纹,有些设备上即使录入了指纹,可是没有开启锁屏password的话此方法还是返回false
*
* @return
*/
private boolean isHasEnrolledFingerprints() {
try {
return mFingerprintManager.hasEnrolledFingerprints();
} catch (Exception e) {
return false;
}
}
/**
* 是否有指纹识别硬件支持
*
* @return
*/
public boolean isHardwareDetected() {
try {
return mFingerprintManager.isHardwareDetected();
} catch (Exception e) {
return false;
}
}
/**
* 推断是否开启锁屏password
*
* @return
*/
private boolean isKeyguardSecure() {
try {
return mKeyManager.isKeyguardSecure();
} catch (Exception e) {
return false;
}
}
/**
* 指纹识别回调接口
*/
public interface IFingerprintResultListener {
void onInSecurity();
void onNoEnroll();
void onSupport();
void onAuthenticateStart();
void onAuthenticateError(int errMsgId, CharSequence errString);
void onAuthenticateFailed();
void onAuthenticateHelp(int helpMsgId, CharSequence helpString);
void onAuthenticateSucceeded(FingerprintManagerCompat.AuthenticationResult result);
}
public void cancelAuthenticate() {
if (mCancellationSignal != null) {
mCancellationSignal.cancel();
mCancellationSignal = null;
}
}
public void onDestroy() {
cancelAuthenticate();
mKeyManager = null;
mFingerprintManager = null;
}
这个对象能够是null,可是这种话。就意味着app无条件信任认证的结果,这个过程可能被攻击。数据能够被篡改。这是app在这种情况下必须承担的风险。
因此。建议这个參数不要置为null。这个类的实例化有点麻烦,主要使用javax的security接口实现。
标签:over oct sig 停止 hand port 引入 系统调用 function
原文地址:http://www.cnblogs.com/gccbuaa/p/7293837.html