Hướng dẫn xây dựng ứng dụng sử dụng API vân tay (FINGERPRINT API) để đăng nhập, đăng ký người dùng trong Android (P1)

Trong hướng dẫn này, chúng ta sẽ học cách sử dụng API vân tay (Fingerprint API) của Android để đăng ký và đăng nhập người dùng. Ngoài ra trong hướng dẫn này cũng hướng dẫn bạn sử dụng SharedPreference, sử dụng Intent để truyền dữ liệu và chuyển activity, sử dụng Dialog, sử dụng Cipher AES để mã hóa dữ liệu…
Android Fingerprint API đã được giới thiệu trong Android 6. Vì vậy, hướng dẫn ví dụ này sẽ sử dụng android min-sdk 23.
Mục tiêu
Chúng ta sẽ tạo Trang đăng ký nơi người dùng lần đầu tiên sẽ điền một thông tin và chọn các tùy chọn về cách xác thực trong quá trình đăng nhập.
Các tùy chọn ở bên dưới.
1. Đăng nhập bằng Xác thực Vân tay
2. Đăng nhập bằng Xác thực vân tay và mật khẩu.
Nếu đăng nhập thành công, người dùng sẽ được đưa đến trang UserProfile và dữ liệu sẽ được hiển thị trong trang.
Nếu bạn chưa sử dụng Android Fingerprint API và bạn muốn tìm hiểu thêm trước khi tiếp tục với hướng dẫn này, chúng ta có thể tìm thấy tài liệu trong hướng dẫn dành cho nhà phát triển Android tại đây .
Một số ảnh chụp màn hình ứng dụng
Table of Contents
1. TẠO MỘT ỨNG DỤNG ANDROID MỚI
Chuyển đến menu File
Click vào New
click vào Android Application
Nhập tên dự án: AndroidFingerPrint
Trọn gói: com.inducesmile.androidfingerprint
Chọn Empty Activity
Đặt tên cho activity của bạn: MainActivity
Giữ các lựa chọn mặc định khác.
Tiếp tục click vào nút Next cho đến khi kết thúc, sau đó click vào Nút Finish.
2. CẬP NHẬT BUILD.GRADLE
Chúng ta sẽ thêm một số plugin của bên thứ ba mà chúng ta sẽ sử dụng trong hướng dẫn này.
Mở gradle.build và thêm mã bên dưới.
apply plugin: 'com.android.application'
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.inducesmile.androidfingerprintlogin"
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.intuit.sdp:sdp-android:1.0.3'
compile 'com.android.support:design:25.0.1'
compile 'com.google.code.gson:gson:2.6.1'
compile 'com.android.support:cardview-v7:25.0.1'
compile 'com.android.support:support-v4:25.0.1'
testCompile 'junit:junit:4.12'
}
3. CẬP NHẬT ANDROIDMANIFEST.XML
Vì chúng ta sẽ sử dụng Android Fingerprint để xác thực người dùng nên chúng ta sẽ thêm quyền của người dùng cho Android FingerPrint. Ngoài ra, chúng ta sẽ sử dụng một lớp CustomApplication sẽ mở rộng từ lớp Application. Thêm mã bên dưới vào file manifest.xml.
4. CẬP NHẬT STRINGS.XML
Chúng ta sẽ cập nhật file string.xml nằm trong thư mục values bên trong thư mục res. Mở file và thêm mã bên dưới đây.
Android FingerPrint Login SIGN IN SIGN UP Error authenticating fingerprint Fingerprint authentication successful Unrecognized permission request " User refused Fingerprint permission" Phone number Address Password Email Username With Fingerprint With Fingerprint and Password LOGIN WITH FINGERPRINT LOGIN
5. CẬP NHẬT COLORS.XML
Mở file color.xml ở cùng vị trí với file string.xml và thêm mã bên dưới.
#d69803 #9f7102 #857300 #367e90 #60c002 #ffffff #000000 #7F7F7F #e6e3e2
6. CẬP NHẬT file BỐ CỤC ACTIVITY_MAIN.XML
Đây là trang giới thiệu sẽ chứa hai nút widget để đăng nhập và đăng ký. Đầu tiên, mở file layout và thêm mã bên dưới.
7. CẬP NHẬT MAINACTIVITY.JAVA
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ActionBar actionBar = getSupportActionBar();
if(null != actionBar){
actionBar.hide();
}
Button signInButton = (Button)findViewById(R.id.sign_in);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(loginIntent);
}
});
Button signUpButton = (Button)findViewById(R.id.sign_up);
signUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent signInIntent = new Intent(MainActivity.this, SignUpActivity.class);
startActivity(signInIntent);
}
});
}
}
8. TẠO VÀ CẬP NHẬT ACTIVITY_SIGN_UP.XML
Tạo một Activity mới sẽ được sử dụng để đăng ký. file layout được đặt tên như trên. Mở file khi nó đã được tạo và thêm mã bên dưới.
9. CẬP NHẬT SIGNUPACTIVITY.JAVA
Trong lớp này, tất cả các giá trị đầu vào của người dùng để đăng ký đều được lấy và lưu trữ trong shared preference.
Mở lớp và thêm mã bên dưới.
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
public class SignUpActivity extends AppCompatActivity {
private static final String TAG = SignUpActivity.class.getSimpleName();
private TextView displayError;
private EditText username;
private EditText email;
private EditText password;
private EditText address;
private EditText phoneNumber;
private RadioGroup radioGroup;
private boolean loginOption;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
setTitle("Android Fingerprint Registration");
username = (EditText)findViewById(R.id.username);
email = (EditText)findViewById(R.id.email);
password = (EditText)findViewById(R.id.password);
address = (EditText)findViewById(R.id.address);
phoneNumber = (EditText)findViewById(R.id.phone_number);
radioGroup = (RadioGroup)findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int id) {
if(id == R.id.with_fingerprint){
loginOption = false;
}
if(id == R.id.with_fingerprint_and_password){
loginOption = true;
}
}
});
Button signUpButton = (Button) findViewById(R.id.sign_up_button);
signUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String usernameValue = username.getText().toString();
String emailValue = email.getText().toString();
String passwordValue = password.getText().toString();
String addressValue = address.getText().toString();
String phonenumberValue = phoneNumber.getText().toString();
int selectedButtonId = radioGroup.getCheckedRadioButtonId();
if(TextUtils.isEmpty(usernameValue) || TextUtils.isEmpty(emailValue)|| TextUtils.isEmpty(passwordValue)
|| TextUtils.isEmpty(addressValue) || TextUtils.isEmpty(phonenumberValue)){
Toast.makeText(SignUpActivity.this, "All input fields must be filled", Toast.LENGTH_LONG).show();
}else if(selectedButtonId == -1){
Toast.makeText(SignUpActivity.this, "Login option must be selected", Toast.LENGTH_LONG).show();
}else{
Gson gson = ((CustomApplication)getApplication()).getGsonObject();
UserObject userData = new UserObject(usernameValue, emailValue, passwordValue, addressValue, phonenumberValue, loginOption);
String userDataString = gson.toJson(userData);
CustomSharedPreference pref = ((CustomApplication)getApplication()).getShared();
pref.setUserData(userDataString);
username.setText("");
email.setText("");
password.setText("");
address.setText("");
phoneNumber.setText("");
Intent loginIntent = new Intent(SignUpActivity.this, LoginActivity.class);
startActivity(loginIntent);
}
}
});
}
}
Ở phần tiếp theo chúng ta sẽ tiếp tục hoàn thiện ứng dụng và sử dụng API vân tay để đăng nhập.
Xem phần tiếp theo tại đây