Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 仿支付寶手勢密碼解鎖功能實作

編輯:關於 Android 程式設計

起步(Starting)

建立手勢密碼可參考 CreateGestureActivity.java 檔案;登入時驗證手勢密碼則可參考 GestureLoginActivity.java 檔案。

功能特色(Features)

本範例使用了 JakeWharton/butterknife(ButterKnife)做視圖綁定,並使用 ACache 來儲存手勢密碼。

 /** * 保存手勢密碼 */ private void saveChosenPattern(List<LockPatternView.Cell> cells) { byte[] bytes = LockPatternUtil.patternToHash(cells); aCache.put(Constant.GESTURE_PASSWORD, bytes); } 

注意(Warning):使用 ACache 類別保存密碼並非永久保存,具體保存期限請參閱 ACache 類別的說明。

密碼保存時採用 SHA 演算法雜湊處理:

 /** * Generate an SHA-1 hash for the pattern. * Not the most secure, but it is at * least a second level of protection. First level is that the file is in a * location only readable by the system process.* * @param pattern * @return the hash of the pattern in a byte array. */ public static byte[] patternToHash(List<LockPatternView.Cell> pattern) { if (pattern == null) { return null; } else { int size = pattern.size(); byte[] res = new byte[size]; for (int i = 0; i < size; i++) { LockPatternView.Cell cell = pattern.get(i); res[i] = (byte) cell.getIndex(); } MessageDigest md = null; try { md = MessageDigest.getInstance("SHA-1"); return md.digest(res); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return res; } } } 

值得一提的是,這裡先把每個節點的 index 轉成 byte 陣列,再用 SHA-1 雜湊,屬於第二層保護;第一層則是檔案存放在只有系統程序能讀取的位置。

此外還可以開啟震動回饋模式——當選中一個節點時,手機會震動:

 /** * Set whether the view will use tactile feedback. *If true, there will be * tactile feedback as the user enters the pattern. * @param tactileFeedbackEnabled Whether tactile feedback is enabled */ public void setTactileFeedbackEnabled(boolean tactileFeedbackEnabled) { mEnableHapticFeedback = tactileFeedbackEnabled; } 

也可以開啟繪製路徑隱藏模式(隱身模式):

 /** * Set whether the view is in stealth mode. If true, there will be no * visible feedback as the user enters the pattern. * @param inStealthMode Whether in stealth mode. */public void setInStealthMode(boolean inStealthMode) { mInStealthMode = inStealthMode; } 

示例(Example)

以上就是本文的全部內容,希望對你在實作手勢密碼解鎖時有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved