fix(app): 修复 ReadCardActivity 资源释放问题

- 在 ReadCardActivity 中添加 onDestroy() 方法
- 在 onDestroy() 中添加资源释放逻辑,包括:
  - 移除 handler 回调  - 解绑 BluetoothLeService  - 关闭 BluetoothLeService 和 BluetoothController
  - 注销 BluetoothController 接收器
  - 关闭 executorService
- 优化资源释放顺序,确保正确关闭和解绑
This commit is contained in:
2025-07-04 17:34:46 +08:00
parent 0943281657
commit 9b5d6fea4b
2 changed files with 27 additions and 0 deletions
+1
View File
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
@@ -616,4 +616,30 @@ public class ReadCardActivity extends AppCompatActivity {
Log.d(TAG, sb.toString());
}
}
@Override
protected void onDestroy() {
Log.d(TAG,"ReadCardActivity onDestroy");
super.onDestroy();
try {
handler.removeCallbacks(readRunnable);
unbindService(mServiceConnection);
if (mBluetoothLeService != null) {
mBluetoothLeService.disconnect();
mBluetoothLeService.close();
mBluetoothLeService = null;
}
if (mBluetoothController != null) {
Log.d(TAG,"释放mBluetoothController====");
mBluetoothController.scanLeDevice(false);
mBluetoothController.close();
mBluetoothController.unRegisterReceiver();
mBluetoothController = null;
}
if (executorService != null && !executorService.isShutdown()) {
executorService.shutdownNow();
}
} catch (Exception e) {
Log.e(TAG,"释放资源时出错\n" + e.getMessage(), e);
}
}
}