Analysis _android of Android fingerprint unlocking method

Source: Internet
Author: User

Let me start by talking about the differences between the two ways.
First Type:
after the transfer of success will not let you unlock the fingerprint, but switch to the original when you set the fingerprint unlock the gesture unlocked page
The second type:
after the transfer is successful, the fingerprint is unlocked. You put your finger directly on the metal sensing ring for fingerprint verification.
We can choose according to the demand

OK, that's a light code.

First type:
XML layout: A text displays a button (does not explain)

Mainactivity.java Source

public class Mainactivity extends fragmentactivity {Fingerprintmanager Manager; 
Keyguardmanager Mkeymanager; 
Private final static int request_code_confirm_device_credentials = 0;

Private final static String TAG = "Finger_log"; 
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Setcontentview (R.layout.activity_main); 1: The first to obtain fingerprint identification of the manager to see whether the acquisition method is the same as the mathematical formula (XXX Manager) =this.getsystemservice (context.xxx service) Manager = (fingerprintmanager) th 
  Is.getsystemservice (Context.fingerprint_service); 
Mkeymanager = (Keyguardmanager) this.getsystemservice (Context.keyguard_service); 2 initialization button Set up listening//listening listening What's to hear? This phone has no fingerprint to recognize this function so look at the Isfinger () button Btn_finger = (Button) Findviewbyid (r.id.btn 
  _activity_main_finger); Btn_finger.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {if Finger ()) {Toast.maketext (mainactivity.this, "Please fingerprint identification", Toast.length_long). Show(); 
        Log (TAG, "keyi"); 
      Startlistening (NULL); 

} 
    } 
  }); public Boolean Isfinger () {//android Studio, no this will make an error if activitycompat.checkselfpermission (this, manifest.pe Rmission. Use_fingerprint)!= packagemanager.permission_granted) {Toast.maketext (this, "no fingerprint recognition permission", Toast.length_short). Show () 
    ; 
  return false; 
  Log (TAG, "have fingerprint permission"); 
    Determine if the hardware supports fingerprint identification if (!manager.ishardwaredetected ()) {Toast.maketext (this, "No fingerprint identification module", Toast.length_short). Show (); 
  return false; 
  Log (TAG, "have fingerprint module");  
    Determine if the lock screen password is open if (!mkeymanager.iskeyguardsecure ()) {Toast.maketext (this, "do not unlock the screen password", Toast.length_short). Show (); 
  return false; 
  Log (TAG, "opened lock screen password"); 
    Determine if there is a fingerprint entry if (!manager.hasenrolledfingerprints ()) {Toast.maketext (this, "No fingerprint Entry", Toast.length_short). Show (); 
  return false; 

  Log (TAG, "entered fingerprint"); 
return true; 
} cancellationsignal mcancellationsignal = new Cancellationsignal (); Callback Method FiNgerprintmanager.authenticationcallback mselfcancelled = new Fingerprintmanager.authenticationcallback () {@Override 
    Enter this method after the public void onauthenticationerror (int errorcode, charsequence errstring) {//But multiple fingerprint password validation errors, and cannot call fingerprint verification in a short time 
    Toast.maketext (Mainactivity.this, errstring, Toast.length_short). Show (); 
  Showauthenticationscreen (); @Override public void onauthenticationhelp (int helpcode, charsequence helpstring) {Toast.maketext (mainacti 
  Vity.this, helpstring, Toast.length_short). Show (); @Override public void onauthenticationsucceeded (Fingerprintmanager.authenticationresult result) {Toast.mak 
  EText (mainactivity.this, "Fingerprint recognition success", Toast.length_short). Show (); @Override public void onauthenticationfailed () {Toast.maketext (mainactivity.this, "Fingerprint recognition failed", Toast.length_sh 
  ORT). Show (); 


} 
}; public void startlistening (Fingerprintmanager.cryptoobject cryptoobject) {//android Studio, no this will make an error if (Activityco Mpat.checkselFpermission (this, Manifest.permission.USE_FINGERPRINT)!= packagemanager.permission_granted) {Toast.maketext (this, 
    "No fingerprint recognition permission," Toast.length_short. Show (); 
  Return 


} manager.authenticate (Cryptoobject, mcancellationsignal, 0, mselfcancelled, NULL); /** * Lock screen Password/private void Showauthenticationscreen () {Intent Intent = Mkeymanager.createconfirmdevicecreden 
  Tialintent ("Finger", "Test fingerprint identification"); 
  if (intent!= null) {Startactivityforresult (intent, request_code_confirm_device_credentials); } @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (Requestcode = RE Quest_code_confirm_device_credentials) {//Challenge completed, proceed with using cipher if (ResultCode = RES 
    ULT_OK) {Toast.maketext (this, "recognition success", Toast.length_short). Show (); 
    else {Toast.maketext (this, "Unrecognized failure", Toast.length_short). Show (); }} private void Log (string tag, string msg) {LOG.D (tag, msg);

 } 
}

===================================

The second type:
1:xml: Layout A text hint two buttons (1: Start identifying 2: Canceling recognition)
2: We put the business operation into a Fingerprintcontroller class

private static Fingerprintcontroller Ssingleton = null;

Private context Mcontext;

Private Fingerprintmanagercompat Manager;
private static final String TAG = "Fingerprintcontroller";

private static final String premission = "Android.permission.USE_FINGERPRINT";
Successful public static final int finger_success = 0;
The hardware does not support public static final int finger_error_no_hardware = 1;
No request permission public static final int finger_error_no_permission = 2; User does not give permission//protection level:normal//fingerprint permission level is normal, in theory does not require dynamic permissions authentication public static final int Finger_error_no_user_
PERMISSION = 3;

User does not store fingerprint public static final int finger_error_no_finger = 4;

Cancel fingerprint identification private cancellationsignal cancellationsignal;

Private Fingerauthlistener Mauthlistener;  public static synchronized Fingerprintcontroller getinstance {if (Ssingleton = = null) {Ssingleton
  = new Fingerprintcontroller (context);
return Ssingleton;
  Public Fingerprintcontroller {mcontext = context; MansAger = Fingerprintmanagercompat.from (This.mcontext); /** * Start fingerprint recognition * After a certain number of failures, you may need to restart/public void Startfingerauth () {if (null = = cancellationsignal) {Canc
  ellationsignal = new Cancellationsignal ();
} manager.authenticate (null, 0, cancellationsignal, New Fingerauthcallback (), NULL); 
    /** * Cancel fingerprint identification/public void Cancelfingerauth () {if (cancellationsignal!= null) {cancellationsignal.cancel ();
  if (Mauthlistener!= null) mauthlistener.cancel (); }/** * Fingerprint recognition callback/public class Fingerauthcallback extends Fingerprintmanagercompat.authenticationcallback {//when present When the error callback this function, such as the failure of multiple attempts, errstring is the error message @Override public void onauthenticationerror (int errmsgid, charsequence errstr 
ing) {//LOG.D (TAG, "onauthenticationerror:" + errstring); 
if (null!= mauthlistener) Mauthlistener.error (errstring.tostring ());
    ///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 before stopping the sensor work @Override public void onauthenticationfailed () { if (nulL!= Mauthlistener) mauthlistener.failure (); 

      @Override public void onauthenticationhelp (int helpmsgid, charsequence helpstring) {if (null!= mauthlistener)
      LOG.E ("Helpmsgid", helpmsgid+ "");
      LOG.E ("helpstring", helpstring.tostring ());
  Mauthlistener.help (Helpstring.tostring ()); This function is recalled when the validated fingerprint succeeds and then no longer listens for fingerprint sensor @Override public void onauthenticationsucceeded (Fingerprintmanagercompat.authen

  Ticationresult result) {if (null!= mauthlistener) mauthlistener.success (); /** * Check fingerprint unlock is available * * @return state/public int checkfingerenable () {if (null = = Manager) {manager = Finge
  Rprintmanagercompat.from (Mcontext);
  } if (!isapppermissionenable ()) {return finger_error_no_permission;
  } if (!manager.ishardwaredetected ()) {return finger_error_no_hardware;
  } if (!manager.hasenrolledfingerprints ()) {return finger_error_no_finger; } if (!isuserpermissionenable ()) {return finger_error_no_user_perMISSION;
return finger_success;
  /** * Whether the permission is declared/private Boolean isapppermissionenable () {Packagemanager pm = Mcontext.getpackagemanager ();
    if (PM = = null) {LOG.W (TAG, "can ' t get Packagemanager");
  return true;
  try {return packagemanager.permission_granted = = Pm.checkpermission (premission, Mcontext.getpackagename ());
    catch (Exception e) {LOG.W (TAG, "can ' t checkt Permission" + e.getmessage ());
  return true; }/** * has dynamic permissions, theoretically does not need to verify/private Boolean isuserpermissionenable () {if (Build.VERSION.SDK_INT >= build.versi On_codes.
  M) {return packagemanager.permission_granted = = Mcontext.checkselfpermission (Manifest.permission.USE_FINGERPRINT);
return true;

public void Setauthlistener (Fingerauthlistener authlistener) {mauthlistener = Authlistener;}

  public interface Fingerauthlistener {void success ();

  void error (String error);

  void Help (String msg);

  void Cancel ();

void failure (); 

 }
}

3: Implement Fingerprintcontroller.fingerauthlistener This callback interface in main

private int code = fingerprintcontroller.finger_success;

Private TextView toast;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

 Setcontentview (R.layout.activity_main);

 Toast = (TextView) Findviewbyid (r.id.toast);
 Code = fingerprintcontroller.getinstance (This). Checkfingerenable ();
  if (code = = fingerprintcontroller.finger_success) {fingerprintcontroller.getinstance (this). Setauthlistener (this);
 Settoast ("Can start fingerprint identification");
    else {switch (code) {Case FingerprintController.FINGER_ERROR_NO_HARDWARE:setToast ("This device does not support fingerprint identification");
   Break
    Case FingerprintController.FINGER_ERROR_NO_PERMISSION:setToast ("Current application does not have fingerprint recognition authority");
   Break
    Case FingerprintController.FINGER_ERROR_NO_FINGER:setToast ("The current device does not enter a fingerprint, please go to the fingerprint");
  Break }} public void start (view view) {if (code = = fingerprintcontroller.finger_success) {Fingerprintcontroller.getin
  Stance (This). Startfingerauth ();
 Settoast ("Start fingerprint identification"); } public VoID Cancel (view view) {if (code = = fingerprintcontroller.finger_success) {fingerprintcontroller.getinstance (this). Canc
 Elfingerauth ();

@Override public void Success () {Settoast ("successful Recognition");}

@Override public void error (String error) {settoast (error);}

@Override public void Help (String msg) {settoast (msg);

@Override public void Cancel () {Settoast ("Cancel fingerprint recognition");}

@Override public void failure () {settoast ("fingerprint recognition failed");
 public void Settoast (String msg) {Toast.settext ("Hint:" + msg);}

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.

Related Article

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.