diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index d625210..22f5861 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -37,5 +37,9 @@
+
diff --git a/app/src/main/java/net/harutiro/trainalert2/MainActivity.kt b/app/src/main/java/net/harutiro/trainalert2/MainActivity.kt
index 5bbece1..b1a8404 100644
--- a/app/src/main/java/net/harutiro/trainalert2/MainActivity.kt
+++ b/app/src/main/java/net/harutiro/trainalert2/MainActivity.kt
@@ -1,28 +1,31 @@
package net.harutiro.trainalert2
+import android.content.Intent
+import android.os.Build
import android.os.Bundle
-import android.util.Log
+//import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
-import androidx.lifecycle.lifecycleScope
-import com.google.android.gms.maps.model.LatLng
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.launch
-import kotlinx.coroutines.withContext
+//import androidx.lifecycle.lifecycleScope
+//import com.google.android.gms.maps.model.LatLng
+//import kotlinx.coroutines.Dispatchers
+//import kotlinx.coroutines.launch
+//import kotlinx.coroutines.withContext
import net.harutiro.trainalert2.core.presenter.FirstPage
-import net.harutiro.trainalert2.features.location.entity.CurrentLocationData
-import net.harutiro.trainalert2.features.location.repository.DistanceJudgement
-import net.harutiro.trainalert2.features.location.repository.LocationRepository
-import net.harutiro.trainalert2.features.notification.api.NotificationApi
-import net.harutiro.trainalert2.features.room.routeDB.repositories.RouteRepository
+//import net.harutiro.trainalert2.features.location.entity.CurrentLocationData
+//import net.harutiro.trainalert2.features.location.repository.DistanceJudgement
+//import net.harutiro.trainalert2.features.location.repository.LocationRepository
+import net.harutiro.trainalert2.features.location.repository.LocationService
+//import net.harutiro.trainalert2.features.notification.api.NotificationApi
+//import net.harutiro.trainalert2.features.room.routeDB.repositories.RouteRepository
import net.harutiro.trainalert2.ui.theme.TrainAlert2Theme
class MainActivity : ComponentActivity() {
- private val routeRepository = RouteRepository()
- private var notificationApi: NotificationApi? = null
- private val locationRepository = LocationRepository()
+// private val routeRepository = RouteRepository()
+// private var notificationApi: NotificationApi? = null
+// private val locationRepository = LocationRepository()
override fun onCreate(savedInstanceState: Bundle?) {
@@ -34,72 +37,77 @@ class MainActivity : ComponentActivity() {
}
}
- // NotificationApiのインスタンスを作成
- notificationApi = NotificationApi(this)
-
- locationRepository.initLocationApi(this)
-
- // 距離判定の処理
- checkDistance()
- }
-
- private fun checkDistance() {
- val judgerange = 600.0 // 判定距離
-
- locationRepository.currentLocationUpdates { CurrentLocationData ->
-
- Log.d("currentLocation", "$CurrentLocationData")
-
- lifecycleScope.launch(Dispatchers.IO) {
- // データベースから目的地の情報を取得
- val routeList = routeRepository.loadAllRoutes() // データベースにあるルート全てを取得する
- if (routeList.isEmpty()) {
- return@launch
- }
-
- val currentLocation = CurrentLocationData (
- CurrentLocationData.latitude,
- CurrentLocationData.longitude
- )
- Log.d("currentLocation", "$currentLocation")
-
- // 全ルートに対して処理を行う
- routeList.forEach { destination ->
- // 目的地を取得
- val destinationLocation = LatLng(
- destination.endLatitude ?: 0.0,
- destination.endLongitude ?: 0.0
- )
-
- // 距離判定を行う
- val isWithinDistance = DistanceJudgement().resultDistance(
- currentLocation.latitude, currentLocation.longitude,
- destinationLocation.latitude, destinationLocation.longitude,
- judgerange // 距離は100m以内か
- )
-
- // 判定がtrueの場合、通知を表示
- if (isWithinDistance) {
- if(destination.alertMethods == 1) {
- withContext(Dispatchers.Main) {
- notificationApi?.showNotification(
- "目的地に近づきました",
- "間もなく到着です!"
- )
- }
- }else if(destination.alertMethods == 2) {
- withContext(
- Dispatchers.Main
- ) {
- notificationApi?.vibrate()
- }
- }
- }
- }
- }
-
+ // サービスの起動
+ val serviceIntent = Intent(this, LocationService::class.java)
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ startForegroundService(serviceIntent)
+ } else {
+ startService(serviceIntent)
}
-
+// // NotificationApiのインスタンスを作成
+// notificationApi = NotificationApi(this)
+//
+// locationRepository.initLocationApi(this)
+//
+// // 距離判定の処理
+// checkDistance()
}
+//
+// private fun checkDistance() {
+// val judgerange = 600.0 // 判定距離
+//
+// locationRepository.currentLocationUpdates { CurrentLocationData ->
+//
+// Log.d("currentLocation", "$CurrentLocationData")
+//
+// lifecycleScope.launch(Dispatchers.IO) {
+// // データベースから目的地の情報を取得
+// val routeList = routeRepository.loadAllRoutes() // データベースにあるルート全てを取得する
+// if (routeList.isEmpty()) {
+// return@launch
+// }
+//
+// val currentLocation = CurrentLocationData (
+// CurrentLocationData.latitude,
+// CurrentLocationData.longitude
+// )
+// Log.d("currentLocation", "$currentLocation")
+//
+// // 全ルートに対して処理を行う
+// routeList.forEach { destination ->
+// // 目的地を取得
+// val destinationLocation = LatLng(
+// destination.endLatitude ?: 0.0,
+// destination.endLongitude ?: 0.0
+// )
+//
+// // 距離判定を行う
+// val isWithinDistance = DistanceJudgement().resultDistance(
+// currentLocation.latitude, currentLocation.longitude,
+// destinationLocation.latitude, destinationLocation.longitude,
+// judgerange // 距離は100m以内か
+// )
+//
+// // 判定がtrueの場合、通知を表示
+// if (isWithinDistance) {
+// if(destination.alertMethods == 1) {
+// withContext(Dispatchers.Main) {
+// notificationApi?.showNotification(
+// "目的地に近づきました",
+// "間もなく到着です!"
+// )
+// }
+// }else if(destination.alertMethods == 2) {
+// withContext(
+// Dispatchers.Main
+// ) {
+// notificationApi?.vibrate()
+// }
+// }
+// }
+// }
+// }
+// }
+// }
}
\ No newline at end of file
diff --git a/app/src/main/java/net/harutiro/trainalert2/features/location/repository/LocationService.kt b/app/src/main/java/net/harutiro/trainalert2/features/location/repository/LocationService.kt
new file mode 100644
index 0000000..37d3107
--- /dev/null
+++ b/app/src/main/java/net/harutiro/trainalert2/features/location/repository/LocationService.kt
@@ -0,0 +1,107 @@
+package net.harutiro.trainalert2.features.location.repository
+
+import android.app.Notification
+import android.app.NotificationChannel
+import android.app.NotificationManager
+import android.app.PendingIntent
+import android.app.Service
+//import android.content.Context
+import android.content.Intent
+import android.os.IBinder
+import android.os.Build
+import androidx.core.app.NotificationCompat
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.launch
+import net.harutiro.trainalert2.features.location.entity.CurrentLocationData
+//import net.harutiro.trainalert2.features.location.repository.DistanceJudgement
+//import net.harutiro.trainalert2.features.location.repository.LocationRepository
+import net.harutiro.trainalert2.features.notification.api.NotificationApi
+import net.harutiro.trainalert2.features.room.routeDB.repositories.RouteRepository
+import com.google.android.gms.maps.model.LatLng
+import kotlinx.coroutines.DelicateCoroutinesApi
+import net.harutiro.trainalert2.MainActivity
+import net.harutiro.trainalert2.R
+
+class LocationService : Service() {
+
+ private val routeRepository = RouteRepository()
+ private val notificationApi = NotificationApi(this)
+ private val locationRepository = LocationRepository()
+
+ override fun onCreate() {
+ super.onCreate()
+ locationRepository.initLocationApi(this)
+ startForegroundService()
+ checkDistance()
+ }
+
+ override fun onBind(intent: Intent?): IBinder? = null
+
+ private fun startForegroundService() {
+ val notificationChannelId = "LocationServiceChannel"
+ val channelName = "Location Service"
+
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ val channel = NotificationChannel(
+ notificationChannelId,
+ channelName,
+ NotificationManager.IMPORTANCE_LOW
+ )
+ val manager = getSystemService(NotificationManager::class.java)
+ manager?.createNotificationChannel(channel)
+ }
+
+ val notification: Notification = NotificationCompat.Builder(this, notificationChannelId)
+ .setContentTitle("TrainAlert2")
+ .setContentText("位置情報を監視中")
+ .setSmallIcon(R.drawable.ic_launcher_foreground)
+ .setContentIntent(PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java), PendingIntent.FLAG_IMMUTABLE))
+ .build()
+
+ startForeground(1, notification)
+ }
+
+ @OptIn(DelicateCoroutinesApi::class)
+ private fun checkDistance() {
+ val judgerange = 600.0 // 判定距離
+
+ locationRepository.currentLocationUpdates { currentLocationData ->
+
+ GlobalScope.launch(Dispatchers.IO) {
+ val routeList = routeRepository.loadAllRoutes()
+ if (routeList.isEmpty()) return@launch
+
+ val currentLocation = CurrentLocationData(
+ currentLocationData.latitude,
+ currentLocationData.longitude
+ )
+
+ routeList.forEach { destination ->
+ val destinationLocation = LatLng(
+ destination.endLatitude ?: 0.0,
+ destination.endLongitude ?: 0.0
+ )
+
+ val isWithinDistance = DistanceJudgement().resultDistance(
+ currentLocation.latitude, currentLocation.longitude,
+ destinationLocation.latitude, destinationLocation.longitude,
+ judgerange
+ )
+
+ if (isWithinDistance) {
+ launch(Dispatchers.Main) {
+ when (destination.alertMethods) {
+ 1 -> notificationApi.showNotification(
+ "目的地に近づきました",
+ "間もなく到着です!"
+ )
+ 2 -> notificationApi.vibrate()
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file