Android のGPS切断からの復帰処理のデバッグ
This commit is contained in:
@ -65,6 +65,26 @@ class LocationService : Service() {
|
|||||||
|
|
||||||
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
|
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デバイスが有効になっているか確認する
|
// GPSデバイスが有効になっているか確認する
|
||||||
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||||
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
||||||
@ -73,6 +93,7 @@ class LocationService : Service() {
|
|||||||
}else{
|
}else{
|
||||||
Log.d("LocationService", "GPS is enabled.")
|
Log.d("LocationService", "GPS is enabled.")
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// フォアグラウンドサービスの設定
|
// フォアグラウンドサービスの設定
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
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 {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
Log.d("LocationService", "Android: onStartCommand.")
|
Log.d("LocationService", "Android: onStartCommand.")
|
||||||
|
|
||||||
|
/* onCreate でやってるので除外。
|
||||||
// 位置情報の権限チェックとGPS有効化の確認を行う
|
// 位置情報の権限チェックとGPS有効化の確認を行う
|
||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||||
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||||
@ -204,6 +226,8 @@ class LocationService : Service() {
|
|||||||
// 位置情報の権限が許可されていない場合の処理を追加する
|
// 位置情報の権限が許可されていない場合の処理を追加する
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
// Foregroundサービスを開始
|
// Foregroundサービスを開始
|
||||||
startForeground(NOTIFICATION_ID, createNotification())
|
startForeground(NOTIFICATION_ID, createNotification())
|
||||||
|
|
||||||
|
|||||||
@ -93,6 +93,7 @@ class MainActivity: FlutterActivity() {
|
|||||||
} else {
|
} else {
|
||||||
Log.d("MainActivity", "Location permission is not granted.")
|
Log.d("MainActivity", "Location permission is not granted.")
|
||||||
// 位置情報の権限が許可されていない場合の処理を追加する
|
// 位置情報の権限が許可されていない場合の処理を追加する
|
||||||
|
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_CODE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -20,7 +20,7 @@ import 'package:shared_preferences/shared_preferences.dart';
|
|||||||
import '../../main.dart';
|
import '../../main.dart';
|
||||||
|
|
||||||
|
|
||||||
class IndexController extends GetxController {
|
class IndexController extends GetxController with WidgetsBindingObserver {
|
||||||
List<GeoJSONFeatureCollection> locations = <GeoJSONFeatureCollection>[].obs;
|
List<GeoJSONFeatureCollection> locations = <GeoJSONFeatureCollection>[].obs;
|
||||||
List<GeoJSONFeature> currentFeature = <GeoJSONFeature>[].obs;
|
List<GeoJSONFeature> currentFeature = <GeoJSONFeature>[].obs;
|
||||||
List<Destination> currentDestinationFeature = <Destination>[].obs;
|
List<Destination> currentDestinationFeature = <Destination>[].obs;
|
||||||
@ -183,6 +183,9 @@ class IndexController extends GetxController {
|
|||||||
_connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
|
_connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
|
||||||
super.onInit();
|
super.onInit();
|
||||||
|
|
||||||
|
WidgetsBinding.instance?.addObserver(this);
|
||||||
|
_startLocationService(); // アプリ起動時にLocationServiceを開始する
|
||||||
|
|
||||||
print('IndexController onInit called'); // デバッグ用の出力を追加
|
print('IndexController onInit called'); // デバッグ用の出力を追加
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -196,9 +199,48 @@ class IndexController extends GetxController {
|
|||||||
@override
|
@override
|
||||||
void onClose() {
|
void onClose() {
|
||||||
_connectivitySubscription.cancel();
|
_connectivitySubscription.cancel();
|
||||||
|
WidgetsBinding.instance?.removeObserver(this);
|
||||||
|
_stopLocationService(); // アプリ終了時にLocationServiceを停止する
|
||||||
super.onClose();
|
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
|
@override
|
||||||
void onReady() async {
|
void onReady() async {
|
||||||
|
|||||||
Reference in New Issue
Block a user