Activity_main.xml Source code
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:gravity= "center" android:orientation= "vertical" android:paddingbottom= "@dimen/activity_ve Rtical_margin "android:paddingleft=" @dimen/activity_horizontal_margin "android:paddingright=" @dimen/activity_ Horizontal_margin "android:paddingtop=" @dimen/activity_vertical_margin "tools:context=" Com.liu.finger.MainActivity "> <textview android:id=" @+id/textview "android:layout_width=" wrap_content
"Android:layout_height=" wrap_content "android:text=" Hello world! " Android:textsize= "18sp"/> <button android:id= "@+id/btn_activity_main_finger" Android:layout_width= "M Atch_parent "android:layout_height=" Wrap_content "android:layout_alignparentstart=" true "Android:layout_below= "@+id/textview" android:layout_margintop= "54DP" android:text= "fingerprint identification"/> </linearlayout> ;
Mainactivity.java Source code
Package Com.liu.finger; Import Android.
Manifest;
Import Android.app.KeyguardManager;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.pm.PackageManager;
Import Android.hardware.fingerprint.FingerprintManager;
Import Android.os.Bundle;
Import android.os.CancellationSignal;
Import Android.support.v4.app.ActivityCompat;
Import android.support.v4.app.FragmentActivity;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.Toast;
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); Manager = (Fingerprintmanager) this.getsystemservice (context.
Fingerprint_service);
Mkeymanager = (Keyguardmanager) this.getsystemservice (Context.keyguard_service);
Button Btn_finger = (button) Findviewbyid (R.id.btn_activity_main_finger);
Btn_finger.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {
if (Isfinger ()) {Toast.maketext (mainactivity.this, "Please fingerprint identification", Toast.length_long). Show ();
Log (TAG, "keyi");
Startlistening (NULL);
}
}
}); public Boolean Isfinger () {//android Studio, no this will be an error if (activitycompat.checkselfpermission) (This, mans Ifest.permission.USE_FINGERPRINT)!= packagemanager.permission_granted) {Toast.maketext (this, "no fingerprint recognition permission", Toast.le
Ngth_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). Sh
ow ();
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 public void Onauthenticationerror (int errorcode, charsequence errstring) {//But multiple fingerprint password validation errors, enter this method;
, cannot call fingerprint verification Toast.maketext (Mainactivity.this, errstring, Toast.length_short) in a short time. Show ();
Showauthenticationscreen (); @Override public void onauthenticationhelp (int helpcode, charsequence helpstring) {Toast.maketext ( Mainactivity.this, HelPstring, Toast.length_short). Show (); @Override public void onauthenticationsucceeded (Fingerprintmanager.authenticationresult result) {T
Oast.maketext (mainactivity.this, "Fingerprint recognition success", Toast.length_short). Show (); @Override public void onauthenticationfailed () {Toast.maketext (mainactivity.this, "Fingerprint recognition failed", Toast.le
Ngth_short). Show ();
}
}; public void startlistening (Fingerprintmanager.cryptoobject cryptoobject) {//android Studio, no this will make an error if (Activi Tycompat.checkselfpermission (this, Manifest.permission.USE_FINGERPRINT)!= packagemanager.permission_granted) {Toa
St.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.createconfirm
Devicecredentialintent ("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 (REQUESTC Ode = = request_code_confirm_device_credentials) {//Challenge completed, proceed with using cipher if (resu
Ltcode = = RESULT_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); }
}
Through this article hope to help develop the Android fingerprint identification friends, thank you for your support to this site!