From b55ee94d8a44b019834c3c5b42345381da237faf Mon Sep 17 00:00:00 2001 From: Sweetbread Date: Thu, 22 May 2025 22:54:51 +0300 Subject: [PATCH] feat: add stop tracking button --- .../geotracker/LocationForegroundService.kt | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/ru/risdeveau/geotracker/LocationForegroundService.kt b/app/src/main/java/ru/risdeveau/geotracker/LocationForegroundService.kt index ec0cedc..c34308b 100644 --- a/app/src/main/java/ru/risdeveau/geotracker/LocationForegroundService.kt +++ b/app/src/main/java/ru/risdeveau/geotracker/LocationForegroundService.kt @@ -5,6 +5,7 @@ import android.Manifest.permission.POST_NOTIFICATIONS import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager +import android.app.PendingIntent import android.app.Service import android.content.Intent import android.os.Build @@ -41,12 +42,14 @@ class LocationForegroundService : Service() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { Log.d("Service", "onStartCommand") - if (!(hasPermission(ACCESS_FINE_LOCATION) + if ( + !(hasPermission(ACCESS_FINE_LOCATION) && ( (Build.VERSION.SDK_INT < 33) || hasPermission(POST_NOTIFICATIONS) - ) - )) { + )) + || intent?.action == ACTION_STOP_SERVICE + ) { stopSelf() return START_NOT_STICKY } @@ -60,6 +63,7 @@ class LocationForegroundService : Service() { } override fun onDestroy() { + Log.d("Service", "Destroyed") locationTracker.stopTracking() super.onDestroy() } @@ -69,8 +73,8 @@ class LocationForegroundService : Service() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel( "location_channel", - "Location Tracking", - NotificationManager.IMPORTANCE_LOW + "Отправка Местоположения", + NotificationManager.IMPORTANCE_HIGH ) val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager manager.createNotificationChannel(channel) @@ -78,12 +82,36 @@ class LocationForegroundService : Service() { } private fun createNotification(): Notification { + val stopIntent = Intent(this, LocationForegroundService::class.java).apply { + action = ACTION_STOP_SERVICE + } + + val stopPendingIntent = PendingIntent.getService( + this, + 0, + stopIntent, + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE + else + PendingIntent.FLAG_UPDATE_CURRENT + ) + return NotificationCompat.Builder(this, "location_channel") .setContentTitle("Отслеживание местоположения") .setContentText("Обновление каждые ${SettingsPreferences.interval} секунд") - .setSmallIcon(R.drawable.ic_launcher_foreground) + .setSmallIcon(R.drawable.share_location) + .addAction( + R.drawable.cancel, + "Остановить", + stopPendingIntent + ) + .setOngoing(true) .build() } override fun onBind(intent: Intent?): IBinder? = null -} \ No newline at end of file + + companion object { + const val ACTION_STOP_SERVICE = "ru.risdeveau.geotracker.STOP_SERVICE" + } +}