fix(app): 修复 ReadCardActivity 资源释放问题
- 在 ReadCardActivity 中添加 onDestroy() 方法 - 在 onDestroy() 中添加资源释放逻辑,包括: - 移除 handler 回调 - 解绑 BluetoothLeService - 关闭 BluetoothLeService 和 BluetoothController - 注销 BluetoothController 接收器 - 关闭 executorService - 优化资源释放顺序,确保正确关闭和解绑
This commit is contained in:
Generated
+1
@@ -1,5 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
|
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||||
<component name="GradleSettings">
|
<component name="GradleSettings">
|
||||||
<option name="linkedExternalProjectsSettings">
|
<option name="linkedExternalProjectsSettings">
|
||||||
<GradleProjectSettings>
|
<GradleProjectSettings>
|
||||||
|
|||||||
@@ -616,4 +616,30 @@ public class ReadCardActivity extends AppCompatActivity {
|
|||||||
Log.d(TAG, sb.toString());
|
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