我先說說這兩種的方式的不同之處吧
第一種:
在調動成功之後 不會讓你指紋解鎖 而是調轉到當初你設定指紋解鎖時的 手勢解鎖頁面
第二種:
在調動成功之後,是進行指紋解鎖 不調轉 你直接把手指放到金屬感應環 上面進行指紋驗證
大家可以根據需求 自行選擇
ok 那就亮代碼了
第一種:
xml 布局: 一個 文本顯示 一個按鈕(不解釋)
MainActivity.java源碼
public class MainActivity extends FragmentActivity { FingerprintManager manager; KeyguardManager mKeyManager; private final static int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 0; private final static String TAG = “finger_log”;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1:先擷取 指紋識別的的管理者 看看擷取方式 是不是感覺就和數學公式一樣 (xxx管理者)=this.getSystemService(Context.xxx服務) manager = (FingerprintManager) this.getSystemService(Context.FINGERPRINT_SERVICE); mKeyManager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE); // 2 初始化按鈕 設定監聽 // 監聽 監聽 聽什麼 肯定是聽聽 這個手機有沒有 指紋識別這個功能 所以 看看isFinger() Button btn_finger = (Button) findViewById(R.id.btn_activity_main_finger); btn_finger.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (isFinger()) { Toast.makeText(MainActivity.this, "請進行指紋識別", Toast.LENGTH_LONG).show(); Log(TAG, "keyi"); startListening(null); } } }); } public boolean isFinger() { //android studio 上,沒有這個會報錯 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "沒有指紋識別許可權", Toast.LENGTH_SHORT).show(); return false; } Log(TAG, "有指紋許可權"); //判斷硬體是否支援指紋識別 if (!manager.isHardwareDetected()) { Toast.makeText(this, "沒有指紋識別模組", Toast.LENGTH_SHORT).show(); return false; } Log(TAG, "有指紋模組"); //判斷 是否開啟鎖屏密碼 if (!mKeyManager.isKeyguardSecure()) { Toast.makeText(this, "沒有開啟鎖屏密碼", Toast.LENGTH_SHORT).show(); return false; } Log(TAG, "已開啟鎖屏密碼"); //判斷是否有指紋錄入 if (!manager.hasEnrolledFingerprints()) { Toast.makeText(this, "沒有錄入指紋", Toast.LENGTH_SHORT).show(); return false; } Log(TAG, "已錄入指紋"); return true; } CancellationSignal mCancellationSignal = new CancellationSignal(); //回調方法 FingerprintManager.AuthenticationCallback mSelfCancelled = new FingerprintManager.AuthenticationCallback() { @Override public void onAuthenticationError(int errorCode, CharSequence errString) { //但多次指紋密碼驗證錯誤後,進入此方法;並且,不能短時間內調用指紋驗證 Toast.makeText(MainActivity.this, errString, Toast.LENGTH_SHORT).show(); showAuthenticationScreen(); } @Override public void onAuthenticationHelp(int helpCode, CharSequence helpString) { Toast.makeText(MainActivity.this, helpString, Toast.LENGTH_SHORT).show(); } @Override public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { Toast.makeText(MainActivity.this, "指紋識別成功", Toast.LENGTH_SHORT).show(); } @Override public void onAuthenticationFailed() { Toast.makeText(MainActivity.this, "指紋識別失敗", Toast.LENGTH_SHORT).show(); } }; public void startListening(FingerprintManager.CryptoObject cryptoObject) { //android studio 上,沒有這個會報錯 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "沒有指紋識別許可權", Toast.LENGTH_SHORT).show(); return; } manager.authenticate(cryptoObject, mCancellationSignal, 0, mSelfCancelled, null); } /** * 鎖屏密碼 */ private void showAuthenticationScreen() { Intent intent = mKeyManager.createConfirmDeviceCredentialIntent("finger", "測試指紋識別"); if (intent != null) { startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) { // Challenge completed, proceed with using cipher if (resultCode == RESULT_OK) { Toast.makeText(this, "識別成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "識別失敗", Toast.LENGTH_SHORT).show(); } } } private void Log(String tag, String msg) { Log.d(tag, msg); } }
===================================
第二種:
1:xml: 布局 一個文本提示 兩個按鈕(1: 開始識別 2:取消識別)
2: 咱們將業務操作放到一個FingerprintController 類裡面
private static FingerprintController sSingleton = null;private Context mContext;private FingerprintManagerCompat manager;private static final String TAG = "FingerprintController";private static final String PREMISSION = "android.permission.USE_FINGERPRINT";//成功public static final int FINGER_SUCCESS = 0;//硬體不支援public static final int FINGER_ERROR_NO_HARDWARE = 1;//沒有申請許可權public static final int FINGER_ERROR_NO_PERMISSION = 2;//使用者沒有賦予許可權//Protection level: normal//指紋許可權的層級是normal,理論上不需要動態許可權認證public static final int FINGER_ERROR_NO_USER_PERMISSION = 3;//使用者沒有儲存指紋public static final int FINGER_ERROR_NO_FINGER = 4;//取消指紋識別private CancellationSignal cancellationSignal;private FingerAuthListener mAuthListener;public static synchronized FingerprintController getInstance(Context context) { if (sSingleton == null) { sSingleton = new FingerprintController(context); } return sSingleton;}public FingerprintController(Context context) { mContext = context; manager = FingerprintManagerCompat.from(this.mContext);}/** * 開始指紋識別 * 失敗次數過多後需要一定時間後才可以重新啟動 */public void startFingerAuth() { if (null == cancellationSignal) { cancellationSignal = new CancellationSignal(); } manager.authenticate(null, 0, cancellationSignal, new FingerAuthCallBack(), null);}/** * 取消指紋識別 */public void cancelFingerAuth() { if (cancellationSignal != null) { cancellationSignal.cancel(); if (mAuthListener != null) mAuthListener.cancel(); }}/** * 指紋識別回調 */public class FingerAuthCallBack extends FingerprintManagerCompat.AuthenticationCallback { // 當出現錯誤的時候回調此函數,比如多次嘗試都失敗了的時候,errString是錯誤資訊 @Override public void onAuthenticationError(int errMsgId, CharSequence errString) {// Log.d(TAG, “onAuthenticationError: ” + errString); if (null != mAuthListener) mAuthListener.error(errString.toString()); } // 當指紋驗證失敗的時候會回調此函數,失敗之後允許多次嘗試,失敗次數過多會停止回應一段時間然後再停止sensor的工作 @Override public void onAuthenticationFailed() { if (null != mAuthListener) mAuthListener.failure(); } @Override public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { if (null != mAuthListener) Log.e("helpMsgId",helpMsgId+""); Log.e("helpString",helpString.toString()); mAuthListener.help(helpString.toString()); } // 當驗證的指紋成功時會回調此函數,然後不再監聽指紋sensor @Override public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) { if (null != mAuthListener) mAuthListener.success(); }}/** * 檢查指紋解鎖是否可用 * * @return 狀態 */public int checkFingerEnable() { if (null == manager) { manager = FingerprintManagerCompat.from(mContext); } if (!isAppPermissionEnable()) { return FINGER_ERROR_NO_PERMISSION; } if (!manager.isHardwareDetected()) { return FINGER_ERROR_NO_HARDWARE; } if (!manager.hasEnrolledFingerprints()) { return FINGER_ERROR_NO_FINGER; } if (!isUserPermissionEnable()) { return FINGER_ERROR_NO_USER_PERMISSION; } return FINGER_SUCCESS;}/** * 是否聲明了該許可權 */private boolean isAppPermissionEnable() { PackageManager pm = mContext.getPackageManager(); if (pm == null) { Log.w(TAG, "can't get packagemanager"); return true; } try { return PackageManager.PERMISSION_GRANTED == pm.checkPermission(PREMISSION, mContext.getPackageName()); } catch (Exception e) { Log.w(TAG, "can't checkt Permission " + e.getMessage()); return true; }}/** * 是否具有動態許可權,理論上不需要驗證 */private boolean isUserPermissionEnable() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return PackageManager.PERMISSION_GRANTED == mContext.checkSelfPermission(Manifest.permission.USE_FINGERPRINT); } return true;}public void setAuthListener(FingerAuthListener authListener) { mAuthListener = authListener;}public interface FingerAuthListener { void success(); void error(String error); void help(String msg); void cancel(); void failure();}}
3:在 main中 實現 FingerprintController.FingerAuthListener這個 回調介面
private int code = FingerprintController.FINGER_SUCCESS;private TextView toast;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toast = (TextView) findViewById(R.id.toast); code = FingerprintController.getInstance(this).checkFingerEnable(); if (code == FingerprintController.FINGER_SUCCESS) { FingerprintController.getInstance(this).setAuthListener(this); setToast("可以啟動指紋識別"); } else { switch (code) { case FingerprintController.FINGER_ERROR_NO_HARDWARE: setToast("該裝置不支援指紋識別"); break; case FingerprintController.FINGER_ERROR_NO_PERMISSION: setToast("當前應用沒有指紋識別許可權"); break; case FingerprintController.FINGER_ERROR_NO_FINGER: setToast("當前裝置沒有錄入指紋,請前往錄入指紋"); break; } }}public void start(View view) { if (code == FingerprintController.FINGER_SUCCESS) { FingerprintController.getInstance(this).startFingerAuth(); setToast("開始指紋識別"); }}public void cancel(View view) { if (code == FingerprintController.FINGER_SUCCESS) { FingerprintController.getInstance(this).cancelFingerAuth(); }}@Overridepublic void success() { setToast("識別成功");}@Overridepublic void error(String error) { setToast(error);}@Overridepublic void help(String msg) { setToast(msg);}@Overridepublic void cancel() { setToast("取消指紋識別");}@Overridepublic void failure() { setToast("指紋識別失敗");}public void setToast(String msg) { toast.setText("提示:" + msg);}
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。