This commit is contained in:
Nedifinita
2025-09-27 00:50:12 +08:00
parent 9b0e9dcacf
commit 8d3366fbf9
7 changed files with 70 additions and 148 deletions

View File

@@ -20,9 +20,9 @@ class BackgroundService {
if (_isInitialized) return;
final service = FlutterBackgroundService();
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
if (Platform.isAndroid) {
const AndroidNotificationChannel channel = AndroidNotificationChannel(
_notificationChannelId,
@@ -34,10 +34,12 @@ class BackgroundService {
playSound: false,
);
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
}
await service.configure(
androidConfiguration: AndroidConfiguration(
onStart: _onStart,
@@ -81,8 +83,9 @@ class BackgroundService {
if (service is AndroidServiceInstance) {
await Future.delayed(const Duration(seconds: 1));
if (await service.isForegroundService()) {
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
final flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
try {
const AndroidNotificationChannel channel = AndroidNotificationChannel(
_notificationChannelId,
@@ -94,8 +97,10 @@ class BackgroundService {
playSound: false,
);
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
await flutterLocalNotificationsPlugin.show(
_notificationId,
@@ -122,10 +127,7 @@ class BackgroundService {
),
),
);
print('前台服务通知显示成功');
} catch (e) {
print('前台服务通知显示失败: $e');
}
} catch (e) {}
}
}
@@ -136,8 +138,9 @@ class BackgroundService {
final bleService = BLEService();
final isConnected = bleService.isConnected;
final deviceStatus = bleService.deviceStatus;
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
final flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin.show(
_notificationId,
'LBJ Console',
@@ -163,9 +166,7 @@ class BackgroundService {
),
),
);
} catch (e) {
print('前台服务通知更新失败: $e');
}
} catch (e) {}
}
}
});
@@ -179,7 +180,7 @@ class BackgroundService {
static Future<void> startService() async {
await initialize();
final service = FlutterBackgroundService();
if (Platform.isAndroid) {
final isRunning = await service.isRunning();
if (!isRunning) {
@@ -208,4 +209,4 @@ class BackgroundService {
service.invoke('setAsBackground');
}
}
}
}

View File

@@ -330,9 +330,7 @@ class BLEService {
_dataController.add(trainRecord);
DatabaseService.instance.insertRecord(trainRecord);
}
} catch (e) {
print("$TAG: JSON Decode Error: $e, Data: $jsonData");
}
} catch (e) {}
}
void _updateConnectionState(bool connected, String status) {

View File

@@ -12,7 +12,8 @@ class LocationService {
LatLng? _currentLocation;
Timer? _locationTimer;
bool _isLocationPermissionGranted = false;
final StreamController<LatLng?> _locationStreamController = StreamController<LatLng?>.broadcast();
final StreamController<LatLng?> _locationStreamController =
StreamController<LatLng?>.broadcast();
Stream<LatLng?> get locationStream => _locationStreamController.stream;
LatLng? get currentLocation => _currentLocation;
@@ -30,7 +31,6 @@ class LocationService {
try {
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
print('定位服务未开启');
return;
}
@@ -40,14 +40,11 @@ class LocationService {
}
if (permission == LocationPermission.deniedForever) {
print('定位权限被拒绝,请在设置中开启');
return;
}
_isLocationPermissionGranted = true;
} catch (e) {
print('请求定位权限失败: $e');
}
} catch (e) {}
}
Future<void> _getCurrentLocation() async {
@@ -56,14 +53,12 @@ class LocationService {
try {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high,
forceAndroidLocationManager: true, // 强制使用Android LocationManager
forceAndroidLocationManager: true,
);
_currentLocation = LatLng(position.latitude, position.longitude);
_locationStreamController.add(_currentLocation);
} catch (e) {
print('获取当前位置失败: $e');
}
} catch (e) {}
}
void _startLocationUpdates() {
@@ -86,4 +81,4 @@ class LocationService {
_locationTimer?.cancel();
_locationStreamController.close();
}
}
}

View File

@@ -9,7 +9,7 @@ class MapStateService {
MapStateService._internal();
static const String _tableName = 'record_map_states';
final Map<String, MapState> _memoryCache = {};
Future<void> _ensureTableExists() async {
@@ -34,10 +34,10 @@ class MapStateService {
Future<void> saveMapState(String key, MapState state) async {
try {
_memoryCache[key] = state;
final db = await DatabaseService.instance.database;
await _ensureTableExists();
await db.insert(
_tableName,
{
@@ -47,9 +47,7 @@ class MapStateService {
},
conflictAlgorithm: ConflictAlgorithm.replace,
);
} catch (e) {
print('保存地图状态失败: $e');
}
} catch (e) {}
}
Future<MapState?> getMapState(String key) async {
@@ -60,7 +58,7 @@ class MapStateService {
try {
final db = await DatabaseService.instance.database;
await _ensureTableExists();
final result = await db.query(
_tableName,
where: 'key = ?',
@@ -74,16 +72,14 @@ class MapStateService {
_memoryCache[key] = state;
return state;
}
} catch (e) {
print('读取地图状态失败: $e');
}
} catch (e) {}
return null;
}
Future<void> deleteMapState(String key) async {
_memoryCache.remove(key);
try {
final db = await DatabaseService.instance.database;
await db.delete(
@@ -91,23 +87,19 @@ class MapStateService {
where: 'key = ?',
whereArgs: [key],
);
} catch (e) {
print('删除地图状态失败: $e');
}
} catch (e) {}
}
Future<void> clearAllMapStates() async {
_memoryCache.clear();
try {
final db = await DatabaseService.instance.database;
await db.delete(_tableName);
} catch (e) {
print('清空地图状态失败: $e');
}
} catch (e) {}
}
void clearMemoryCache() {
_memoryCache.clear();
}
}
}

View File

@@ -90,20 +90,32 @@ class NotificationService {
String _buildNotificationContent(TrainRecord record) {
final buffer = StringBuffer();
buffer.writeln('车次: ${record.fullTrainNumber}');
buffer.writeln('线路: ${record.route}');
buffer.writeln('方向: ${record.directionText}');
buffer.write(record.fullTrainNumber);
if (_isValidValue(record.route)) {
buffer.write(' ${record.route}');
}
if (_isValidValue(record.directionText)) {
buffer.write(' ${record.directionText}');
}
if (_isValidValue(record.positionInfo)) {
buffer.write(' ${record.positionInfo}');
}
buffer.writeln();
if (_isValidValue(record.locoType) && _isValidValue(record.loco)) {
final shortLoco = record.loco.length > 5
? record.loco.substring(record.loco.length - 5)
: record.loco;
buffer.write('${record.locoType}-$shortLoco');
} else if (_isValidValue(record.locoType)) {
buffer.write(record.locoType);
} else if (_isValidValue(record.loco)) {
buffer.write(record.loco);
}
if (_isValidValue(record.speed)) {
buffer.writeln('速度: ${record.speed} km/h');
buffer.write(' ${record.speed}km/h');
}
if (_isValidValue(record.positionInfo)) {
buffer.writeln('位置: ${record.positionInfo}');
}
buffer.writeln('时间: ${record.formattedTime}');
return buffer.toString().trim();
}