ภาพรวม
ตัวอย่างนี้สาธิตวิธีตรวจสอบว่าผู้ใช้ได้ลงทะเบียนลายนิ้วมือไว้ในอุปกรณ์ Android หรือยัง โดยใช้คลาส FingerprintManager ซึ่งเหมาะสำหรับแอปที่ต้องการยืนยันตัวตนด้วยชีววิทยา (Biometric Authentication)
ขั้นตอนการสร้างโปรเจกต์
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่ใน Android Studio
เปิด Android Studio → File → New Project → เลือก Empty Activity → กรอกรายละเอียดโปรเจกต์ → Finish
ขั้นตอนที่ 2: จัดการ Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/actionEvent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:textSize="40sp" />
</LinearLayout>
ใช้ TextView เพื่อแสดงผลสถานะการลงทะเบียนลายนิ้วมือ
ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/MainActivity.java)
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@SuppressLint("RestrictedApi")
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView actionEvent = findViewById(R.id.actionEvent);
FingerprintManager fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
// ตรวจสอบว่ามีลายนิ้วมือลงทะเบียนหรือไม่
boolean hasEnrolled = fingerprintManager.hasEnrolledFingerprints();
actionEvent.setText(String.valueOf(hasEnrolled));
}
}
หมายเหตุ: ใช้ Build.VERSION_CODES.M (API 23) เนื่องจาก FingerprintManager เพิ่มเข้ามาตั้งแต่ Android 6.0
ขั้นตอนที่ 4: เพิ่ม Permission ใน AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
การทดสอบ
รันแอปบนอุปกรณ์จริง (เพราะ Emulator ไม่รองรับเซ็นเซอร์ลายนิ้วมือจริง) ผลลัพธ์จะแสดง true หากมีลายนิ้วมือลงทะเบียนไว้ หรือ false หากยังไม่มี
ข้อควรระวัง
- คลาส
FingerprintManagerถูก Deprecated ใน API 28 ควรใช้BiometricPromptแทนสำหรับโปรเจกต์ใหม่ - ต้องทดสอบบนอุปกรณ์จริงที่มีเซ็นเซอร์ลายนิ้วมือ
- Permission
USE_FINGERPRINTเป็น Normal permission จึงไม่ต้องขอ Runtime Permission