Để triển khai chức năng kích hoạt phần mềm thông qua mã kích hoạt trên máy tính bảng Android, chúng tôi gặp phải thách thức khi mã kích hoạt gồm 40 ký tự khiến việc nhập liệu thủ công trở nên phức tạp. Giải pháp tối ưu được lựa chọn là sử dụng QR Code để truyền tải thông tin kích hoạt, kết hợp với thư viện ZXing để quét mã hiệu quả trên thiết bị Huawei không hỗ trợ Google Services.
Trong tập tin AndroidManifest.xml cần khai báo:
1
2
3
4
5
6
7
|
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature
android:name="android.hardware.camera.front"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
|
2. Thêm thư viện hỗ trợ
Cập nhật Gradle dependency:
1
2
|
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
implementation 'com.google.zxing:core:3.5.2'
|
3. Triển khai trong Fragment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
class QrScanFragment : Fragment() {
private lateinit var scanProcessor: IntentIntegrator
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
setupQRScanner()
return binding.root
}
private fun setupQRScanner() {
scanProcessor = IntentIntegrator.forSupportFragment(this).apply {
setDesiredBarcodeFormats(IntentIntegrator.QR_CODE)
setPrompt("Đưa mã QR vào vùng quét")
setCameraDistance(150) // Thiết lập khoảng cách tiêu cự
setBeepEnabled(false) // Tắt âm thanh quét
}
}
private fun triggerScan() {
scanProcessor.initiateScan()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
val scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
scanResult?.let {
if (it.contents.isNullOrEmpty()) {
showNotification("Mã QR không chứa dữ liệu")
} else {
handleActivationCode(it.contents)
}
} ?: super.onActivityResult(requestCode, resultCode, data)
}
private fun handleActivationCode(code: String) {
// Xử lý xác thực mã kích hoạt
if(ActivationValidator.isValid(code)) {
navigateToSuccessScreen()
} else {
showInvalidCodeAlert()
}
}
}
|
4. Tùy chỉnh giao diện quét QR
Để cải thiện trải nghiệm người dùng:
- Tạo lớp
CustomCaptureActivity
kế thừa CaptureActivity
- Thiết kế layout tùy chỉnh với:
- Nút thoát (ImageView) ở góc trên bên trái
- Hiệu ứng animation đường quét laser
- Thanh trạng thái hiển thị tiến trình quét
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<!-- custom_qr_scanner.xml -->
<LinearLayout>
<FrameLayout
android:id="@+id/preview_view"
android:layout_width="match_parent"
android:layout_height="300dp" />
<ImageView
android:id="@+id/btn_exit"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/ic_close"
android:layout_gravity="top|end"
android:padding="8dp" />
<TextView
android:id="@+id/tv_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Đang chuẩn bị..."
android:layout_gravity="bottom|center_horizontal"
android:padding="16dp" />
</LinearLayout>
|
5. Mẹo tối ưu hiệu suất
- Thêm logic tự động điều chỉnh độ sáng camera
- Triển khai bộ đếm thời gian quét tối đa 30 giây
- Hỗ trợ quét ngược sáng với chế độ low-light
- Tiền xử lý hình ảnh trước khi nhận diện
6. Kiểm tra đa thiết bị
Đảm bảo kiểm tra trên các dòng máy Huawei MatePad, Samsung Tab S và Google Pixel C để xác minh tính tương thích. Đặc biệt lưu ý xử lý ngoại lệ khi thiết bị không có camera hỗ trợ AF (Auto Focus).
7.
- ZXing Android Embedded Documentation
- QR Code Optimization Guide
- Custom Camera View Implementation
Giải pháp này giúp giảm 80% thời gian nhập liệu thủ công, đồng thời nâng cao trải nghiệm người dùng trên các thiết bị Android không hỗ trợ Google MLKit.