if (fingerprintManager.isHardwareDetected()) {
//判断屏幕锁是否正常。想要使用指纹识别的话,必须首先使能屏幕锁才行,
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
// this device is secure..
/* Return whether the keyguard is secured by a PIN, pattern or password or a SIM card
* is currently locked.*/
// if (keyguardManager.isKeyguardSecure()) {
//6.0中,普通app要想使用指纹识别功能的话,用户必须首先在setting中注册至少一个指纹才行,否则是不能使用的
if (fingerprintManager.hasEnrolledFingerprints()) {
// start fingerprint auth here.
try {
// CryptoObjectHelper cryptoObjectHelper = new CryptoObjectHelper();
// 创建一个取消信号,初始化时是非取消状态
if (cancellationSignal == null) {
cancellationSignal = new CancellationSignal();
}
if (cancellationSignal.isCanceled()) {
cancellationSignal = new CancellationSignal();
}
//指纹认证回调接口
myAuthCallback = new MyAuthCallback(context, handler);
//一直扫描指纹硬件。
fingerprintManager.authenticate(null, cancellationSignal, 0, myAuthCallback, null);
} catch (Exception e) {
}
}
以上是指纹验证的主要代码,写在Activity中是可以正常验证的,写在Service却拿不到返回值,不知道是什么原因?
http://blog.csdn.net/zhangdong305/article/details/52438050
过去这么久了,不知道你是否还需要,但还是回复下吧,希望对其它人有帮助。
跟踪framework层源码,在FingerprintService发现有个方法(canUseFingerprint)判断是否允许进程扫描指纹,继续跟踪发现会调用到函数isForegroundActivity,源码如下:
private boolean isForegroundActivity(int uid, int pid) {
try {
List procs =
ActivityManagerNative.getDefault().getRunningAppProcesses();
int N = procs.size();
for (int i = 0; i < N; i++) {
RunningAppProcessInfo proc = procs.get(i);
if (proc.pid == pid && proc.uid == uid
&& proc.importance == IMPORTANCE_FOREGROUND) {
return true;
}
}
} catch (RemoteException e) {
Slog.w(TAG, "am.getRunningAppProcesses() failed");
}
return false;
}
三方应用如果没有启动Activity的话,那么其所属进程的等级就不是 IMPORTANCE_FOREGROUND等级的,所以这里返回false,继而canUseFingerprint返回false,那么authenticate方法实际并没有执行成功。