Compare commits
2 Commits
515f950bde
...
4d86cd0630
Author | SHA1 | Date | |
---|---|---|---|
4d86cd0630 | |||
b55ee94d8a |
@ -17,8 +17,8 @@ android {
|
|||||||
applicationId = "ru.risdeveau.geotracker"
|
applicationId = "ru.risdeveau.geotracker"
|
||||||
minSdk = 24
|
minSdk = 24
|
||||||
targetSdk = 35
|
targetSdk = 35
|
||||||
versionCode = 1
|
versionCode = 2
|
||||||
versionName = "1.0"
|
versionName = "1.1"
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import android.Manifest.permission.POST_NOTIFICATIONS
|
|||||||
import android.app.Notification
|
import android.app.Notification
|
||||||
import android.app.NotificationChannel
|
import android.app.NotificationChannel
|
||||||
import android.app.NotificationManager
|
import android.app.NotificationManager
|
||||||
|
import android.app.PendingIntent
|
||||||
import android.app.Service
|
import android.app.Service
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
@ -41,12 +42,14 @@ class LocationForegroundService : Service() {
|
|||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
Log.d("Service", "onStartCommand")
|
Log.d("Service", "onStartCommand")
|
||||||
|
|
||||||
if (!(hasPermission(ACCESS_FINE_LOCATION)
|
if (
|
||||||
|
!(hasPermission(ACCESS_FINE_LOCATION)
|
||||||
&& (
|
&& (
|
||||||
(Build.VERSION.SDK_INT < 33)
|
(Build.VERSION.SDK_INT < 33)
|
||||||
|| hasPermission(POST_NOTIFICATIONS)
|
|| hasPermission(POST_NOTIFICATIONS)
|
||||||
)
|
))
|
||||||
)) {
|
|| intent?.action == ACTION_STOP_SERVICE
|
||||||
|
) {
|
||||||
stopSelf()
|
stopSelf()
|
||||||
return START_NOT_STICKY
|
return START_NOT_STICKY
|
||||||
}
|
}
|
||||||
@ -60,6 +63,7 @@ class LocationForegroundService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
|
Log.d("Service", "Destroyed")
|
||||||
locationTracker.stopTracking()
|
locationTracker.stopTracking()
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
@ -69,8 +73,8 @@ class LocationForegroundService : Service() {
|
|||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
val channel = NotificationChannel(
|
val channel = NotificationChannel(
|
||||||
"location_channel",
|
"location_channel",
|
||||||
"Location Tracking",
|
"Отправка Местоположения",
|
||||||
NotificationManager.IMPORTANCE_LOW
|
NotificationManager.IMPORTANCE_HIGH
|
||||||
)
|
)
|
||||||
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||||
manager.createNotificationChannel(channel)
|
manager.createNotificationChannel(channel)
|
||||||
@ -78,12 +82,36 @@ class LocationForegroundService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun createNotification(): Notification {
|
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")
|
return NotificationCompat.Builder(this, "location_channel")
|
||||||
.setContentTitle("Отслеживание местоположения")
|
.setContentTitle("Отслеживание местоположения")
|
||||||
.setContentText("Обновление каждые ${SettingsPreferences.interval} секунд")
|
.setContentText("Обновление каждые ${SettingsPreferences.interval} секунд")
|
||||||
.setSmallIcon(R.drawable.ic_launcher_foreground)
|
.setSmallIcon(R.drawable.share_location)
|
||||||
|
.addAction(
|
||||||
|
R.drawable.cancel,
|
||||||
|
"Остановить",
|
||||||
|
stopPendingIntent
|
||||||
|
)
|
||||||
|
.setOngoing(true)
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBind(intent: Intent?): IBinder? = null
|
override fun onBind(intent: Intent?): IBinder? = null
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val ACTION_STOP_SERVICE = "ru.risdeveau.geotracker.STOP_SERVICE"
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user