iOS GPS 設定によるポップアップ、バックグラウンド処理修正。Android は未確認
This commit is contained in:
@ -7,6 +7,8 @@
|
||||
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
|
||||
<uses-permission android:name="android.permission.CAMERA"/>
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<application
|
||||
android:label="岐阜ナビ"
|
||||
android:name="${applicationName}"
|
||||
@ -39,5 +41,10 @@
|
||||
android:value="2" />
|
||||
<meta-data android:name="com.google.android.geo.API_KEY"
|
||||
android:value="AIzaSyAUBI1ablMKuJwGj2-kSuEhvYxvB1A-mOE"/>
|
||||
<service
|
||||
android:name="com.example.rogapp.LocationService"
|
||||
android:enabled="true"
|
||||
android:exported="false"
|
||||
android:foregroundServiceType="location" />
|
||||
</application>
|
||||
</manifest>
|
||||
|
||||
@ -0,0 +1,98 @@
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.IBinder
|
||||
import androidx.core.app.NotificationCompat
|
||||
|
||||
class LocationService : Service() {
|
||||
|
||||
override fun onBind(intent: Intent?): IBinder? {
|
||||
return null
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
// フォアグラウンドサービスの設定
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val channel = NotificationChannel(
|
||||
"location",
|
||||
"Location",
|
||||
NotificationManager.IMPORTANCE_LOW
|
||||
)
|
||||
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
val notification = NotificationCompat.Builder(this, "location")
|
||||
.setContentTitle("Tracking location...")
|
||||
.setContentText("Location: null")
|
||||
.setSmallIcon(R.drawable.ic_launcher)
|
||||
.setOngoing(true)
|
||||
.build()
|
||||
startForeground(1, notification)
|
||||
}
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
// バックグラウンドでの位置情報取得処理を実装
|
||||
val locationSettings = LocationSettings.Builder()
|
||||
.setAccuracy(LocationAccuracy.HIGH)
|
||||
.setDistanceFilter(100.0f)
|
||||
.build()
|
||||
|
||||
try {
|
||||
Geolocator.getPositionStream(locationSettings)
|
||||
.catch { e ->
|
||||
// エラーハンドリング
|
||||
println("Location Error: $e")
|
||||
null
|
||||
}
|
||||
.filterNotNull()
|
||||
.filter { position ->
|
||||
// GPS信号がlow以上の場合のみ記録
|
||||
position.accuracy <= 100
|
||||
}
|
||||
.onEach { position ->
|
||||
val lat = position.latitude
|
||||
val lng = position.longitude
|
||||
val timestamp = System.currentTimeMillis()
|
||||
|
||||
// データベースに位置情報を保存
|
||||
addGPStoDB(lat, lng, timestamp)
|
||||
}
|
||||
.launchIn(CoroutineScope(Dispatchers.IO))
|
||||
} catch (e: Exception) {
|
||||
println("Error starting background tracking: $e")
|
||||
}
|
||||
|
||||
return START_STICKY
|
||||
}
|
||||
|
||||
private fun addGPStoDB(lat: Double, lng: Double, isCheckin: Int = 0) {
|
||||
try {
|
||||
val context = applicationContext
|
||||
val preferences = context.getSharedPreferences("RogPreferences", Context.MODE_PRIVATE)
|
||||
val teamName = preferences.getString("team_name", "") ?: ""
|
||||
val eventCode = preferences.getString("event_code", "") ?: ""
|
||||
|
||||
if (teamName.isNotEmpty() && eventCode.isNotEmpty()) {
|
||||
val gpsData = GpsData(
|
||||
id = 0,
|
||||
team_name = teamName,
|
||||
event_code = eventCode,
|
||||
lat = lat,
|
||||
lon = lng,
|
||||
is_checkin = isCheckin,
|
||||
created_at = System.currentTimeMillis()
|
||||
)
|
||||
|
||||
GlobalScope.launch {
|
||||
val db = GpsDatabaseHelper.getInstance(context)
|
||||
db.insertGps(gpsData)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
println("Error adding GPS data to DB: $e")
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2,5 +2,32 @@ package com.example.rogapp
|
||||
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
|
||||
import androidx.annotation.NonNull
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
|
||||
class MainActivity: FlutterActivity() {
|
||||
private val CHANNEL = "location"
|
||||
|
||||
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
|
||||
super.configureFlutterEngine(flutterEngine)
|
||||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
|
||||
call, result ->
|
||||
when (call.method) {
|
||||
"startLocationService" -> {
|
||||
val intent = Intent(this, LocationService::class.java)
|
||||
startService(intent)
|
||||
result.success(null)
|
||||
}
|
||||
"stopLocationService" -> {
|
||||
val intent = Intent(this, LocationService::class.java)
|
||||
stopService(intent)
|
||||
result.success(null)
|
||||
}
|
||||
else -> {
|
||||
result.notImplemented()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user