iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 29
0
Software Development

Kotlin 2018連續開發30天系列 第 29

# Kotlin 2018連續開發30天 day29 指紋辨識加上進度條

  • 分享至 

  • xImage
  •  

這兩樣在這一期的鐵人賽中都有提過 今天把這兩樣加起來

先上Activity程式碼

class fingerprint2 : AppCompatActivity() {

var mkeyguardManager : KeyguardManager? =null
var mFingerprintManager : FingerprintManager? = null
var CancellationSignal: CancellationSignal? = null


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_fingerprint2)
    mkeyguardManager = getSystemService(Activity.KEYGUARD_SERVICE) as KeyguardManager
    mFingerprintManager = getSystemService(FingerprintManager::class.java)

    if (!mkeyguardManager!!.isDeviceSecure()){
        Toast.makeText(this, "Secure lock screen hasn't set up.\n" + "Go to 'Settings -> Security -> Fingerprint' to set up a fingerprint",
                Toast.LENGTH_LONG).show()
        return;
    }
    if(!mFingerprintManager!!.isHardwareDetected()){
        Toast.makeText(this, "No Fingerprint reader", Toast.LENGTH_LONG).show()
        return
    }
    if(!mFingerprintManager!!.hasEnrolledFingerprints()){
        Toast.makeText(this, "Go to 'Settings -> Security -> Fingerprint' and register at least one fingerprint",
                Toast.LENGTH_LONG).show()
        return
    }

    startListing()


}


override fun onPause() {
    super.onPause()
    CancellationSignal!!.cancel()
    CancellationSignal =null
}

private val mHandler_1= @SuppressLint("HandlerLeak")
object:Handler(){
    override fun handleMessage(msg: Message?) {
        when(msg!!.what){
            1-> {
                tpc_progress2.setProgress(count.num,100f)
            }
        }
        super.handleMessage(msg)

    }
}
private val fingerprint_task = object: Thread() {

    override fun run() {
        super.run()
        for (i in 0 ..100){
            try {

                if(count.num == 50){
                    Thread.sleep(5000)
                }

                Thread.sleep(3)
                val msg = Message()
                msg.what = 1
                count.num = i
                mHandler_1.sendMessage(msg)





            }catch (e:InterruptedException){
                e.printStackTrace()
            }

        }

    }
}

fun startListing() {
    CancellationSignal = CancellationSignal()
    mFingerprintManager!!.authenticate(null, CancellationSignal, 0,
            object: FingerprintManager.AuthenticationCallback() {


                override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
                    super.onAuthenticationError(errorCode, errString)
                    Log.e("", "error " + errorCode + " " + errString);
                }

                override fun onAuthenticationSucceeded(result: FingerprintManager.AuthenticationResult?) {
                    super.onAuthenticationSucceeded(result)
                    Log.i("", "onAuthenticationSucceeded")
                    tpc_progress2.setOnNumchange(100)

                    fingerprint2_text.text = "指紋辨識成功"
                    fingerprint2_text.visibility = View.VISIBLE
                    val t = Thread(fingerprint_task)
                    t.start()

                }

                override fun onAuthenticationFailed() {
                    super.onAuthenticationFailed()
                    Log.e("", "onAuthenticationFailed")
                    fingerprint2_text.text = "指紋辨識失敗"
                }
            },null)
}

}

XML

<com.example.likunlin.firebase7.TextProgressCircle
android:id="@+id/tpc_progress2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />

<TextView
    android:id="@+id/fingerprint2_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:text="指紋辨識"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="@+id/tpc_progress2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
    

我會把這兩樣加起來一起用 看起來就比較像真的使用指紋辨識器的樣子


上一篇
Kotlin 2018連續開發30天 day 28 超簡易3d gallery使用方法
下一篇
Kotlin 2018連續開發30天 day30 Notification
系列文
Kotlin 2018連續開發30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言