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"
+ }
+}
diff --git a/app/src/main/res/drawable/cancel.xml b/app/src/main/res/drawable/cancel.xml
new file mode 100644
index 0000000..f030bdd
--- /dev/null
+++ b/app/src/main/res/drawable/cancel.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/app/src/main/res/drawable/share_location.xml b/app/src/main/res/drawable/share_location.xml
new file mode 100644
index 0000000..264d318
--- /dev/null
+++ b/app/src/main/res/drawable/share_location.xml
@@ -0,0 +1,9 @@
+
+
+