前言:对于指纹识别,大多数开发者可能想到的是API 23以后,Android自带的指纹识别sdk,Google对于指纹识别一直保持谨慎态度,至少在笔者看来好像显得有些保守。
往往一些新技术的类似指纹解锁或者是屏幕压感几乎都是由手机产商开发的,记得在华为荣耀5x的机子上曾经实验过指纹解锁,但是原生API是不提供的,更遗憾的是华为也没有提供相应sdk,使得除了官方之外,第三方开发者并没有接触的渠道。
作为指纹解锁的试水者之一,三星在5.x时代已经大规模使用这门技术,而且惊喜的是将sdk开放出来让开发者使用,下面就讲讲大致的使用过程,有不对的地方,欢迎指正,一起学习提高。
1、首先是引入三星官方sdk:
http://developer.samsung.com/galaxy/pass
讲下载的两个包加入工程。包并不是很大,对apk大小影响不大
2、manifest文件中添加权限:
<uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY"/>
3、接下来就没什么好说了,一切尽在代码中
public class MainActivity extends AppCompatActivity {
private SpassFingerprint mSpassFingerprint;
private Spass mSpass;
private boolean isFeatureEnabled_index = false;
private boolean onReadyIdentify = false;
private boolean isFeatureEnabled_fingerprint = false;
private boolean needRetryIdentify = false;
private ArrayList<Integer> designatedFingers = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpass = new Spass();
try {
mSpass.initialize(MainActivity.this);
isFeatureEnabled_fingerprint = mSpass.isFeatureEnabled(Spass.DEVICE_FINGERPRINT);
} catch (SsdkUnsupportedException e) {
Log.e("Yankee","Exception: " + e);
} catch (UnsupportedOperationException e) {
Toast.makeText(getApplicationContext(),"当前机型不支持指纹....",Toast.LENGTH_SHORT).show();
Log.e("Yankee","Fingerprint Service is not supported in the device");
}
if (isFeatureEnabled_fingerprint) {
mSpassFingerprint = new SpassFingerprint(MainActivity.this);
Log.e("Yankee","Fingerprint Service is supported in the device.");
Log.e("Yankee","SDK version : " + mSpass.getVersionName());
} else {
Toast.makeText(getApplicationContext(),"当前机型不支持指纹....",Toast.LENGTH_SHORT).show();
Log.e("Yankee","Fingerprint Service is not supported in the device.");
return;
}
findViewById(R.id.identify).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startIdentify();
}
});
}
private void setIdentifyIndex() {
if (isFeatureEnabled_index) {
if (mSpassFingerprint != null && designatedFingers != null) {
mSpassFingerprint.setIntendedFingerprintIndex(designatedFingers);
}
}
}
private void startIdentify() {
if (onReadyIdentify == false) {
try {
onReadyIdentify = true;
if (mSpassFingerprint != null) {
setIdentifyIndex();
mSpassFingerprint.startIdentify(mIdentifyListener);
}
if (designatedFingers != null) {
Log.e("Yankee","Please identify finger to verify you with " + designatedFingers.toString() + " finger");
} else {
Log.e("Yankee","Please identify finger to verify you");
}
} catch (SpassInvalidStateException ise) {
onReadyIdentify = false;
resetIdentifyIndex();
if (ise.getType() == SpassInvalidStateException.STATUS_OPERATION_DENIED) {
Log.e("Yankee","Exception: " + ise.getMessage());
}
} catch (IllegalStateException e) {
onReadyIdentify = false;
resetIdentifyIndex();
Log.e("Yankee","Exception: " + e);
}
} else {
Log.e("Yankee","The previous request is remained. Please finished or cancel first");
}
}
private SpassFingerprint.IdentifyListener mIdentifyListener = new SpassFingerprint.IdentifyListener() {
@Override
public void onFinished(int eventStatus) {
//Log.e("Yankee","identify finished : reason =" + getEventStatusName(eventStatus));
int FingerprintIndex = 0;
String FingerprintGuideText = null;
try {
FingerprintIndex = mSpassFingerprint.getIdentifiedFingerprintIndex();
} catch (IllegalStateException ise) {
Log.e("Yankee",ise.getMessage());
}
if (eventStatus == SpassFingerprint.STATUS_AUTHENTIFICATION_SUCCESS) {
Toast.makeText(getApplicationContext(),"Success....",Toast.LENGTH_SHORT).show();
Log.e("Yankee","onFinished() : Identify authentification Success with FingerprintIndex : " + FingerprintIndex);
} else if (eventStatus == SpassFingerprint.STATUS_AUTHENTIFICATION_PASSWORD_SUCCESS) {
Log.e("Yankee","onFinished() : Password authentification Success");
} else if (eventStatus == SpassFingerprint.STATUS_OPERATION_DENIED) {
Log.e("Yankee","onFinished() : Authentification is blocked because of fingerprint service internally.");
} else if (eventStatus == SpassFingerprint.STATUS_USER_CANCELLED) {
Log.e("Yankee","onFinished() : User cancel this identify.");
} else if (eventStatus == SpassFingerprint.STATUS_TIMEOUT_FAILED) {
Log.e("Yankee","onFinished() : The time for identify is finished.");
Toast.makeText(getApplicationContext(),"Fail....",Toast.LENGTH_SHORT).show();
} else if (eventStatus == SpassFingerprint.STATUS_QUALITY_FAILED) {
Log.e("Yankee","onFinished() : Authentification Fail for identify.");
Toast.makeText(getApplicationContext(),"Fail....",Toast.LENGTH_SHORT).show();
needRetryIdentify = true;
FingerprintGuideText = mSpassFingerprint.getGuideForPoorQuality();
Toast.makeText(getApplicationContext(), FingerprintGuideText, Toast.LENGTH_SHORT).show();
} else {
Log.e("Yankee","onFinished() : Authentification Fail for identify");
Toast.makeText(getApplicationContext(),"Fail....",Toast.LENGTH_SHORT).show();
needRetryIdentify = true;
}
if (!needRetryIdentify) {
resetIdentifyIndex();
}
}
@Override
public void onReady() {
Log.e("Yankee","identify state is ready");
}
@Override
public void onStarted() {
Log.e("Yankee","User touched fingerprint sensor");
}
@Override
public void onCompleted() {
Log.e("Yankee","the identify is completed");
onReadyIdentify = false;
if (needRetryIdentify) {
needRetryIdentify = false;
//mHandler.sendEmptyMessageDelayed(MSG_AUTH, 100);
}
}
};
private void resetIdentifyIndex() {
designatedFingers = null;
}
}