Я пытаюсь перезапустить службы переднего плана после того, как пользователь очистил список последних приложений или память. Теперь приложение принудительно убило, ничего не показывая. Я думаю, что этот метод onDestroy не работает над задачей верхнего упоминания. Пожалуйста, дайте мне лучшее решение.
Ключевой момент: необходимо перезапустить службу после того, как пользователь очистит последнее приложение. Я знаю, что последнее обновление ограничило некоторые из этих услуг. Есть ли какие-либо решения, пожалуйста, помогите мне!
Что я пробовал,
Служба переднего плана
import android.Manifest
import android.app.AlarmManager
import android.app.PendingIntent
import android.app.Service
import android.app.job.JobInfo
import android.app.job.JobScheduler
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.location.LocationManager
import android.net.Uri
import android.os.Build
import android.os.IBinder
import android.os.Looper
import android.os.PowerManager
import android.os.SystemClock
import android.provider.Settings
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.Priority
import com.google.android.gms.location.LocationAvailability
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationResult
class ForegroundService: Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
createForegroundNotification()
Log.d("MyPersistentsService","Services started")
return START_STICKY
}
override fun onDestroy() {
super.onDestroy()
scheduleRestart()
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
private fun scheduleRestart() {
val alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
val restartIntent = Intent(this, ForegroundService::class.java)
val pendingIntent = PendingIntent.getService(
this,
0,
restartIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val triggerAtMillis = System.currentTimeMillis() + 60 * 1000
alarmManager.set(
AlarmManager.RTC_WAKEUP,
triggerAtMillis,
pendingIntent
)
Log.d("Service","Restart in 60 seconds")
}
private fun createForegroundNotification(){
val notification = NotificationCompat.Builder(this,"persistent_service_channel")
.setContentTitle("Foreground Service")
.setContentText("Service Running")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.build()
startForeground(1, notification)
}
} ```
Подробнее здесь: https://stackoverflow.com/questions/784 ... nd-service
Функция onDestroy() не работает в службе переднего плана ⇐ Android
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Исключение Android 14 при запуске службы переднего плана с типами переднего плана
Anonymous » » в форуме Android - 0 Ответы
- 60 Просмотры
-
Последнее сообщение Anonymous
-