編輯:關於Android編程
我先說說這兩種的方式的不同之處吧
第一種:
在調動成功之後 不會讓你指紋解鎖 而是調轉到當初你設置指紋解鎖時的 手勢解鎖頁面
第二種:
在調動成功之後,是進行指紋解鎖 不調轉 你直接把手指放到金屬感應環 上面進行指紋驗證
大家可以根據需求 自行選擇
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; @Override protected 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(); } } @Override public void success() { setToast("識別成功"); } @Override public void error(String error) { setToast(error); } @Override public void help(String msg) { setToast(msg); } @Override public void cancel() { setToast("取消指紋識別"); } @Override public void failure() { setToast("指紋識別失敗"); } public void setToast(String msg) { toast.setText("提示:" + msg); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
在Android應用開發中,當我們開發完軟件之後,我們不希望別人能夠反編譯破解我們的應用程序,不能修改我們的代碼邏輯。實際上,在應用程序的安全機制考慮中,我們希望自己的應
學習下,記錄下,分享下 init主進程啟動,解析init.rc後依次exec fork啟動相關的進程,其中以class main和core為首的service將會率
MediaRecorder類詳解 手機一般都有麥克風和攝像頭,而Android系統就可以利用這些硬件來錄制音視頻了。 為了增加對錄制音視頻的支持,Android系統提
一、源代碼源代碼及demo二、背景先看看Win10的加載動畫(找了很久才找到):每次打開電腦都會有這個加載動畫,看上挺cool的,就想著自己能否實現它。要實現這個動畫?首