2017-03-03 22 views
1

工作,我嘗試利用指紋API構建一個demo,像這樣:指紋無法在服務

 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) { 

      } 

     } 

,並在活動組件的工作方式,我可以分辨我的指紋。 但是當我嘗試使用這些代碼與服務或BroadcastReciver組件的工作,我不能接受任何的回調,是不是正確的指紋API可以在活動組件只使用?這是爲什麼?

+0

你需要顯示的UI用戶告訴他,把他的手指,你可以從你的服務 – LaurentY

+0

我用服務創造了一個浮動視圖來告訴用戶把他的手指.The問題是>> fingerprintManager活動啓動.authenticate(NULL,cancellationSignal,0,myAuthCallback,NULL); >>我得到一無所知的結果,如果我在服務中使用這些代碼,但它在活動的作品,我想弄明白。 –

回答

0

我已經找到了從源代碼答案,

/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; 
    } 

它需要一個前臺活動來驗證!