Android fingerprint unlock example code
Android 6.0 and above systems support fingerprint identification and unlocking function: it is used in the project, so it is hereby extracted as a memo.
The function is as follows: the fingerprint identification interface will pop up when the user switches the app to the background (for more than a certain time, say 30 seconds) and then enters the program. The user enters the fingerprint and the unlock is successful. The fingerprint identification module is actually very simple. Google's API has been encapsulated. We just need to learn how to call it.
Idea:
When the user switches the program to the background, there needs to be a method for timing. Where is this method written? Yes, it should be written in the service. To start a service in an activity:
intent.setpackage(getPackageName); This sentence must be added to start services in systems 6.0 and above.
Don't forget to register the service in the manifest file.
Start a thread in the service with onstart():
The timing uses a while loop to make the thread sleep for one second. Ha ha, it's not easy to use handler.
Determine whether the program is running in the background:
There is a small detail. When the user switches from the background to the foreground, but the user does not unlock, he switches to the background. After 30 seconds, he switches to the foreground. At this time, he can't jump out of the gesture unlocking interface.
Solution: 1. Set the start mode of the gesture interface. 2. Check the current running interface of the program.
Let's talk about the API of fingerprint identification: under V4 packages 6.0 and above:
You need to add permissions to the manifest file:
The class used is fingerprintmanagercompat. The API in this class can detect whether your mobile phone has a fingerprint sensor and whether the mobile phone has entered a fingerprint.
Authenticationcallback can inherit this class and rewrite the methods in it to receive fingerprint recognition results:
For details, you can download the following demo
Address: Demo
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.