gpt4 book ai didi

android - 指纹无法在服务中工作

转载 作者:行者123 更新时间:2023-11-30 00:41:07 24 4
gpt4 key购买 nike

我尝试使用 FingerPrint API 构建一个演示,如下所示:

        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 组件中工作,我可以区分我的指纹。但是当我尝试将这些代码与服务或 BroadcastReciver 组件一起使用时,我无法收到任何回调,FingerPrint API 只能在 Activity 组件中使用吗?为什么?

最佳答案

我已经从源码中找到了答案,

/frameworks/base/services/core/java/com/android/server/fingerprint/Fingerprint/FingerprintService.java

      @Override // Binder call
public void authenticate(final IBinder token, final long opId, final int groupId,
final IFingerprintServiceReceiver receiver, final int flags,
final String opPackageName) {
if (!canUseFingerprint(opPackageName, true /* foregroundOnly */)) {
if (DEBUG) Slog.v(TAG, "authenticate(): reject " + opPackageName);
return;
}

……

private boolean canUseFingerprint(String opPackageName, boolean foregroundOnly) {
checkPermission(USE_FINGERPRINT);
final int uid = Binder.getCallingUid();
final int pid = Binder.getCallingPid();
if (opPackageName.equals(mKeyguardPackage)) {
return true; // Keyguard is always allowed
}
if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
Slog.w(TAG,"Rejecting " + opPackageName + " ; not a current user or profile");
return false;
}
if (mAppOps.noteOp(AppOpsManager.OP_USE_FINGERPRINT, uid, opPackageName)
!= AppOpsManager.MODE_ALLOWED) {
Slog.w(TAG, "Rejecting " + opPackageName + " ; permission denied");
return false;
}
if (foregroundOnly && !isForegroundActivity(uid, pid)) {
Slog.w(TAG, "Rejecting " + opPackageName + " ; not in foreground");
return false;
}
return true;
}


private boolean isForegroundActivity(int uid, int pid) {
try {
List<RunningAppProcessInfo> 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 进行身份验证!!

关于android - 指纹无法在服务中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42574605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com