Android のバックグラウンドGPSを組み込み

This commit is contained in:
2024-05-06 00:06:38 +09:00
parent 7a97127a19
commit dd9343bef7
89 changed files with 521 additions and 273 deletions

View File

@ -52,7 +52,12 @@ void saveGameState() async {
Get.find<DestinationController>();
IndexController indexController = Get.find<IndexController>();
SharedPreferences pref = await SharedPreferences.getInstance();
pref.setInt("user_id", indexController.currentUser[0]["user"]["id"]);
debugPrint("indexController.currentUser = ${indexController.currentUser}");
if(indexController.currentUser.isNotEmpty) {
pref.setInt("user_id", indexController.currentUser[0]["user"]["id"]);
}else{
debugPrint("User is empty....");
}
pref.setBool("is_in_rog", destinationController.isInRog.value);
pref.setBool(
"rogaining_counted", destinationController.rogainingCounted.value);
@ -79,6 +84,8 @@ void restoreGame() async {
SharedPreferences pref = await SharedPreferences.getInstance();
IndexController indexController = Get.find<IndexController>();
int? savedUserId = pref.getInt("user_id");
//int? currUserId = indexController.currentUser[0]['user']['id'];
//debugPrint("savedUserId=${savedUserId}, currentUser=${currUserId}");
if (indexController.currentUser.isNotEmpty &&
indexController.currentUser[0]["user"]["id"] == savedUserId) {
DestinationController destinationController =
@ -127,11 +134,15 @@ void main() async {
// startMemoryMonitoring(); // 2024-4-8 Akira: メモリ使用量のチェックを開始 See #2810
Get.put(SettingsController()); // これを追加
/*
runZonedGuarded(() {
runApp(const ProviderScope(child: MyApp()));
}, (error, stackTrace) {
ErrorService.reportError(error, stackTrace, deviceInfo);
});
*/
runApp(const ProviderScope(child: MyApp()));
//runApp(const MyApp());
}
@ -307,6 +318,11 @@ Future<void> stopBackgroundTracking() async {
debugPrint("バックグラウンド処理:停止しました。");
await positionStream?.cancel();
positionStream = null;
}else if(Platform.isAndroid && background==true){
background=false;
debugPrint("バックグラウンド処理:停止しました。");
await positionStream?.cancel();
positionStream = null;
}
}
@ -354,38 +370,54 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
// Get.find<DestinationController>();
switch (state) {
case AppLifecycleState.resumed:
// Foreground に戻った時の処理
debugPrint(" ==(Status Changed)==> RESUMED. フォアグラウンドに戻りました");
locationController.resumePositionStream();
//print("RESUMED");
restoreGame();
// バックグラウンド処理を停止
// バックグラウンド処理を停止
if (Platform.isIOS && destinationController.isRunningBackgroundGPS) {
// Foreground に戻った時の処理
debugPrint(" ==(Status Changed)==> RESUMED. フォアグラウンドに戻りました");
locationController.resumePositionStream();
//print("RESUMED");
restoreGame();
stopBackgroundTracking();
destinationController.isRunningBackgroundGPS=false;
destinationController.restartGPS();
} else if (Platform.isAndroid) {
const platform = MethodChannel('location');
platform.invokeMethod('stopLocationService');
} else if (Platform.isAndroid ) {
if( destinationController.isRunningBackgroundGPS ){
const platform = MethodChannel('location');
platform.invokeMethod('stopLocationService');
destinationController.isRunningBackgroundGPS=false;
destinationController.restartGPS();
debugPrint("stopped android location service..");
}
debugPrint("==(Status Changed)==> RESUMED. android フォアグラウンドに戻りました");
locationController.resumePositionStream();
//print("RESUMED");
restoreGame();
}else{
debugPrint("==(Status Changed)==> RESUMED 不明状態");
}
break;
case AppLifecycleState.inactive:
// アプリが非アクティブになったときに発生します。
// これは、別のアプリやシステムのオーバーレイ(着信通話やアラームなど)によって一時的に中断された状態です。
debugPrint(" ==(Status Changed)==> PAUSED. 非アクティブ処理。");
//locationController.resumePositionStream();
// 追加: フロントエンドのGPS信号のlistenを停止
locationController.stopPositionStream();
if (Platform.isIOS && !destinationController.isRunningBackgroundGPS) { // iOSはバックグラウンドでもフロントの処理が生きている。
// これは、別のアプリやシステムのオーバーレイ(着信通話やアラームなど)によって一時的に中断された状態です。
debugPrint(" ==(Status Changed)==> INACTIVE. 非アクティブ処理。");
//locationController.resumePositionStream();
// 追加: フロントエンドのGPS信号のlistenを停止
locationController.stopPositionStream();
if (Platform.isIOS ) { // iOSはバックグラウンドでもフロントの処理が生きている。
destinationController.isRunningBackgroundGPS=true;
startBackgroundTracking();
}else if(Platform.isAndroid){
const platform = MethodChannel('location');
platform.invokeMethod('startLocationService');
}else if(Platform.isAndroid && !destinationController.isRunningBackgroundGPS){
debugPrint(" ==(Status Changed)==> INACTIVE. 非アクティブ処理。");
}else{
debugPrint("==(Status Changed)==> INACTIVE 不明状態");
}
saveGameState();
break;
@ -393,6 +425,19 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
// バックグラウンドに移行したときの処理
//locationController.resumePositionStream();
debugPrint(" ==(Status Changed)==> PAUSED. バックグラウンド処理。");
if (Platform.isIOS && !destinationController.isRunningBackgroundGPS) {
debugPrint("iOS already running background GPS processing when it's inactive");
} else if(Platform.isAndroid && !destinationController.isRunningBackgroundGPS) {
debugPrint(
" ==(Status Changed)==> PAUSED. Android バックグラウンド処理。");
locationController.stopPositionStream();
const platform = MethodChannel('location');
platform.invokeMethod('startLocationService');
//platform.invokeMethod('stopLocationService');
destinationController.isRunningBackgroundGPS = true;
//startBackgroundTracking();
}
saveGameState();
break;
case AppLifecycleState.detached: