Android. Останавливается сервис после некоторого времени
Использую сервис для отклонения входящих звонков. Сервис завершает работу примерно через час после запуска. В чём может быть проблема? Или как сделать так, чтобы сервис работал, пока его не отключит пользователь в приложении? замена START_STICKY на START_REDELIVER_INTENT не помогла Код сервиса представлен ниже: `
private lateinit var notificationManager: NotificationManager
override fun onCreate() {
super.onCreate()
//получаем notificationManager для отображения уведомления
notificationManager = this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
//показываем уведомление о работе приложения в фоновом процессе
sendNotification("Запущен фоновый процесс", "Номера из \"Белого списка\" могут вам звонить.")
//какой-то процесс
doTask()
return START_STICKY
}
private fun doTask(){
//что-то делаем
Log.i("CALL OFF. INFO", "SERVICE IS RUNNING")
}
private fun sendNotification(title: String, text: String){
//показываем уведомление
//активити, которое будет работать в фоне
val notificationIntent = Intent(this, MainActivity::class.java)
notificationIntent.action = Intent.ACTION_MAIN
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER)
//PendingIntent (что будем показывать при нажатии на уведомление)
val contentIntent = PendingIntent.getActivity(
applicationContext,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT
)
//если версия SDK > O , то создаём канал для получения уведомлений
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel(StaticVars().NOTIFICATION_NAME.toString(), StaticVars().NOTIFICATION_NAME);
}
//как только канал создан, показываем уведомление
val builder = NotificationCompat
.Builder(this, StaticVars().NOTIFICATION_NAME)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentIntent(contentIntent)//что открываем при нажатии на уведомление
.setOngoing(true)//убираем возможность удаления уведомления
.setContentTitle(title) // название
.setContentText(text) // текст
.setAutoCancel(false)
.setPriority(NotificationCompat.PRIORITY_HIGH) // приоритет
//создаём уведомление
val notificationManager = NotificationManagerCompat.from(this)
notificationManager.notify(StaticVars().NOTIFICATION_ID, builder.build())
val notification : Notification
notification = builder.build()
//запускаем фоновый процесс
startForeground(StaticVars().NOTIFICATION_ID, notification)
}
@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(channelId: String, channelName: String): String? {
val chan = NotificationChannel(
channelId,
channelName, NotificationManager.IMPORTANCE_NONE
)
chan.lightColor = Color.BLUE
chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(chan)
return channelId
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onDestroy() {
super.onDestroy()
//удаляем напоминание при завершении работы сервиса
notificationManager.cancel(StaticVars().NOTIFICATION_ID)
}
`