fix(app): 修复 ReadCardActivity 资源释放问题
- 在 ReadCardActivity 中添加 onDestroy() 方法 - 在 onDestroy() 中添加资源释放逻辑,包括: - 移除 handler 回调 - 解绑 BluetoothLeService - 关闭 BluetoothLeService 和 BluetoothController - 注销 BluetoothController 接收器 - 关闭 executorService - 优化资源释放顺序,确保正确关闭和解绑
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user