Android Fingerprint Identification API preliminary _android

Source: Internet
Author: User
Tags genymotion free

Google has officially supported fingerprint recognition after android6.0, and is still on holiday today, so I tried this API at random, but encountered a variety of problems
① found a lot of problems in using the Fingerprintmanager class, and some of the functions in this class were hide, which we can't call, like Enroll (), which means that the current official support is actually limited, We can read a fingerprint that already exists on this machine (for unlocking), then verify the fingerprints, but not allow the user to enter a fingerprint in app use, for other features of the app, which is a flaw. For the moment, the following figure shows the ability to identify a fingerprint that can be unlocked.
② use of Fingerprintmanager will encounter in the application to determine the permissions of the problem, it is not clear because the Android m required or API needs, the call when there is no response, neither function, nor prompted to give permission, the reason to be verified.
③ found two special classes after a fruitless attempt, one called Fingerprintmanagercompat, a compatible fingerprint operation class, and a similar one called FingerPrintManagerCompatApi23, The use of these two classes can also achieve the ability to identify fingerprints, but there are limitations, as ① said. All two classes can do this, but API level 23 is required, and if it doesn't, the classes do nothing.
Show diagram, because there is no real machine, so can only use the simulator debugging, here also can only use the official, genymotion free version seemingly can not simulate fingerprints:


Directly on the code:

public class Mainactivity extends Appcompatactivity implements View.onclicklistener {private static final String TAG =
 "Mainactivity";
 Private Button check;

 Private Fingerprintmanagercompat Manager;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

 Setcontentview (R.layout.activity_main);

 Check = (Button) Findviewbyid (R.id.btn_check);

 Check.setonclicklistener (this);
 Gets an instance of Fingerprintmanagercompat Manager = Fingerprintmanagercompat.from (this); @Override public void OnClick (View v) {switch (V.getid ()) {case R.id.btn_check:/** * Start validation, when to stop determined by the system, if validation succeeds , then the system will be related to sensor, if it fails, allow * Multiple attempts, if it still fails, it will be rejected for a period of time, and then close the sensor, after a period of time again allow the attempt * * * The fourth parameter is focused, you need to pass in a Fingerprintmanagercompa
 T.authenticationcallback * and write some methods, different situations callback different functions * * manager.authenticate (NULL, 0, NULL, new Mycallback (), NULL);
 Break } public class Mycallback extends Fingerprintmanagercompat.authenticationcallback {private static final String TAG = "Mycallback"; Callback this function when an error occurs, such as when multiple attempts fail, errstring is the error message @Override public void onauthenticationerror (int errmsgid, charsequence
 errstring) {log.d (TAG, "onauthenticationerror:" + errstring); ///When the fingerprint verification fails, the function is recalled, the failure allows multiple attempts, and the number of failures stops responding for a period of time and then stops sensor work @Override public void onauthenticationfailed () {LOG.D
 (TAG, "onauthenticationfailed:" + "validation Failure"); @Override public void onauthenticationhelp (int helpmsgid, charsequence helpstring) {log.d (TAG, "Onauthenticationhel
 P: "+ helpstring); This function is recalled when the validated fingerprint succeeds and then no longer listens for fingerprint sensor @Override public void onauthenticationsucceeded (
 Fingerprintmanagercompat.authenticationresult result) {LOG.D (TAG, "onauthenticationsucceeded:" + "validation Successful"); 

 }
 }

}

A friend raised a question, which is recorded here:
how can sensor continue to listen for new fingerprints after failing or succeeding?
A: Because the API is newer, this compatible manager class does not have the ability to restart automatically, but we can write one ourselves. Because the API stipulates that if the error or succeed method is called back, the sensor will be closed until the next call to the Authenticate method authorization, but we cannot call this method directly in error or succeed because of security considerations, Developers are not allowed to have continuous authorization for a short period of time, after a rough test, Android allows us to reopen the sensor authorization monitor after 30s, so what we're going to do is send a deferred message via the handler sendmessagedelayed method, Again, the Authenticate method is called again in handler, with the following specific code:

Private Handler Handler = new Handler () {
 @Override public
 void Handlemessage (msg) {
 Super.handlemessage (msg);
 LOG.D (TAG, "Handlemessage: Restart fingerprint module");
 Manager.authenticate (NULL, 0, NULL, new Mycallback (), handler);
 }
; 
@Override public
void onauthenticationerror (int errmsgid, charsequence errstring) {
 handler.sendmessagedelayed (New Message (), 1000 *);
 LOG.D (TAG, "onauthenticationerror:" + errstring);
}

@Override public
void onauthenticationsucceeded (fingerprintmanagercompat.authenticationresult result) {
 handler.sendmessagedelayed (New Message (), 1000 *);
 LOG.D (TAG, "onauthenticationsucceeded:" + "validation Successful");
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.