iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 21
0
Mobile Development

iOS Developer Learning Android系列 第 21

iOS Developer Learning Android. Lesson 21 - 指紋辨識 (不能靠臉我靠雙手可以吧)

今天講一下怎麼用古早的Fingerprint來做指紋辨識(因為我的手機還是安卓6)
新玩意BiometricPrompt不在本日守備範圍內

本日效果

⚠️⚠️⚠️可以看到Android並沒有提供標準的TouchID UI....(或許用BiometricPrompt就會有?)

程式實作

  1. 在manifest加上權限申請 <uses-permission android:name="android.permission.USE_FINGERPRINT" />

  2. 主要用到兩個Class

    1. FingerprintManager//檢查跟辨識使用
    2. CancellationSignal//取消辨識使用

      要選import android.os.CancellationSignal;
  3. 先檢查

        if (checkSelfPermission(Manifest.permission.USE_FINGERPRINT) == PackageManager.PERMISSION_GRANTED)
        {
            fingerprintManager = (FingerprintManager)getSystemService(Activity.FINGERPRINT_SERVICE);
            if (!fingerprintManager.isHardwareDetected())
            {
                textView.setText("您的裝置不支援指紋辨識功能");
                return;
            }

            if (!fingerprintManager.hasEnrolledFingerprints())
            {
                textView.setText("您尚未設定指紋");
                return;
            }
        }
        else
        {
            textView.setText("請允許檢查指紋");
            return;
        }
  1. 後辨識
    1. call 他去辨識 fingerprintManager.authenticate(null,cancellationSignal,0,mAuthenticationCallback,null);
    FingerprintManager.AuthenticationCallback mAuthenticationCallback
            = new FingerprintManager.AuthenticationCallback()
    {
        @Override
        public void onAuthenticationError(int errorCode, CharSequence errString)
        {
            timer.cancel();
            textView.setText("辨識錯誤\n" + errorCode + "\n" + errString);
        }

        @Override
        public void onAuthenticationFailed()
        {
            textView.setText("辨識失敗");
        }

        @Override
        public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result)
        {
            timer.cancel();
            textView.setText("辨識通過");
        }
    };

如何使用模擬器測試指紋辨識

跟iOS不同⚠️⚠️⚠️必須先做一些設定才能測

  1. 先在模擬器的設定裡添加指紋,指紋不能單獨存在,我是選Pattern螢幕圖形鎖
  2. 然後就要開始設定指紋囉
  3. 這時候要透過adb來加入指紋:adb -e emu finger touch {自取ID}
  4. 成功設定了,當要辨識的時候一樣輸入上述指令配上正確的ID就會成功,不同ID就失敗

-bash: adb: command not found

代表你ADB(Android Debug Bridge)還沒設定好

  1. 先確認~/Library/Android/sdk/platform-tools裡有沒有adb
  2. 在Terminal 輸入
    $ touch .bash_profile
    $ open -e .bash_profile
    會開啟一個純文字檔
  3. 輸入export PATH=${PATH}:~/Library/Android/sdk/platform-tools並存檔
  4. 然後指令 $ source .bash_profile
  5. 再打adb應該就不會not found了

參考資料

今天的範例程式

可以去 https://github.com/mark33699/IDLA 看一下順便給顆⭐️


如果你喜歡我的影片別忘了按讚分享加訂閱,開啟紅色的小鈴鐺,我們明天見~


上一篇
iOS Developer Learning Android. Lesson 20 - Activity Gallery (不用再尋找或比較套件了,官方佛心內建常用UI Design Pattern)
下一篇
iOS Developer Learning Android. Lesson 22 - Google Map (讀萬卷書、行萬里路)
系列文
iOS Developer Learning Android30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
MarkFly~
iT邦新手 4 級 ‧ 2019-10-07 00:38:33

updated

我要留言

立即登入留言