From e70d3fd012e98453f04f5b56f84bc82e68799da3 Mon Sep 17 00:00:00 2001 From: Akira Date: Thu, 16 May 2024 10:02:13 +0900 Subject: [PATCH] =?UTF-8?q?Android=20=E3=81=AEGPS=E5=88=87=E6=96=AD?= =?UTF-8?q?=E3=81=8B=E3=82=89=E3=81=AE=E5=BE=A9=E5=B8=B0=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=81=AE=E3=83=87=E3=83=90=E3=83=83=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dvox/gifunavi/LocationService.kt | 24 ++++++++++ .../kotlin/com/dvox/gifunavi/MainActivity.kt | 1 + lib/pages/index/index_controller.dart | 44 ++++++++++++++++++- 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/android/app/src/main/kotlin/com/dvox/gifunavi/LocationService.kt b/android/app/src/main/kotlin/com/dvox/gifunavi/LocationService.kt index 0eb3875..e73914d 100644 --- a/android/app/src/main/kotlin/com/dvox/gifunavi/LocationService.kt +++ b/android/app/src/main/kotlin/com/dvox/gifunavi/LocationService.kt @@ -65,6 +65,26 @@ class LocationService : Service() { fusedLocationClient = LocationServices.getFusedLocationProviderClient(this) + // 位置情報の権限チェックとGPS有効化の確認を行う + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { + val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager + if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { + val locationRequest = LocationRequest.create().apply { + priority = LocationRequest.PRIORITY_HIGH_ACCURACY + interval = 10000 + fastestInterval = 5000 + } + fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null) + } else { + Log.d("LocationService", "GPS is disabled.") + // GPSが無効の場合の処理を追加する(例: ユーザーにGPSを有効にするように促すなど) + } + } else { + Log.d("LocationService", "Location permission is not granted.") + // 位置情報の権限が許可されていない場合の処理を追加する + } + + /* // GPSデバイスが有効になっているか確認する val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { @@ -73,6 +93,7 @@ class LocationService : Service() { }else{ Log.d("LocationService", "GPS is enabled.") } + */ // フォアグラウンドサービスの設定 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -185,6 +206,7 @@ class LocationService : Service() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { Log.d("LocationService", "Android: onStartCommand.") + /* onCreate でやってるので除外。 // 位置情報の権限チェックとGPS有効化の確認を行う if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager @@ -204,6 +226,8 @@ class LocationService : Service() { // 位置情報の権限が許可されていない場合の処理を追加する } + */ + // Foregroundサービスを開始 startForeground(NOTIFICATION_ID, createNotification()) diff --git a/android/app/src/main/kotlin/com/dvox/gifunavi/MainActivity.kt b/android/app/src/main/kotlin/com/dvox/gifunavi/MainActivity.kt index ea4b328..1011834 100644 --- a/android/app/src/main/kotlin/com/dvox/gifunavi/MainActivity.kt +++ b/android/app/src/main/kotlin/com/dvox/gifunavi/MainActivity.kt @@ -93,6 +93,7 @@ class MainActivity: FlutterActivity() { } else { Log.d("MainActivity", "Location permission is not granted.") // 位置情報の権限が許可されていない場合の処理を追加する + ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_CODE) } } diff --git a/lib/pages/index/index_controller.dart b/lib/pages/index/index_controller.dart index 9d91d8d..6d77184 100644 --- a/lib/pages/index/index_controller.dart +++ b/lib/pages/index/index_controller.dart @@ -20,7 +20,7 @@ import 'package:shared_preferences/shared_preferences.dart'; import '../../main.dart'; -class IndexController extends GetxController { +class IndexController extends GetxController with WidgetsBindingObserver { List locations = [].obs; List currentFeature = [].obs; List currentDestinationFeature = [].obs; @@ -183,6 +183,9 @@ class IndexController extends GetxController { _connectivity.onConnectivityChanged.listen(_updateConnectionStatus); super.onInit(); + WidgetsBinding.instance?.addObserver(this); + _startLocationService(); // アプリ起動時にLocationServiceを開始する + print('IndexController onInit called'); // デバッグ用の出力を追加 } @@ -196,9 +199,48 @@ class IndexController extends GetxController { @override void onClose() { _connectivitySubscription.cancel(); + WidgetsBinding.instance?.removeObserver(this); + _stopLocationService(); // アプリ終了時にLocationServiceを停止する super.onClose(); } + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + + if (state == AppLifecycleState.resumed) { + if (!_isLocationServiceRunning()) { + _startLocationService(); + } + } else if (state == AppLifecycleState.paused) { + _stopLocationService(); + } + } + + bool _isLocationServiceRunning() { + // LocationServiceが実行中かどうかを確認する処理を実装する + // 例えば、SharedPreferencesにサービスの状態を保存するなど + // ここでは簡単のために、常にfalseを返すようにしています + return false; + } + + void _startLocationService() async { + const platform = MethodChannel('location'); + try { + await platform.invokeMethod('startLocationService'); + } on PlatformException catch (e) { + print("Failed to start location service: '${e.message}'."); + } + } + + void _stopLocationService() async { + const platform = MethodChannel('location'); + try { + await platform.invokeMethod('stopLocationService'); + } on PlatformException catch (e) { + print("Failed to stop location service: '${e.message}'."); + } + } + /* @override void onReady() async {