Microphone foreground service is almost always killed by certain operating systems

Viewed 338

I simply cannot understand why my phone kills my foreground service.

Relevant code

In AndroidManifest:

    <service
        android:name=".recorder.RecorderService"
        android:exported="false"
        android:process=":Recorder"
        android:foregroundServiceType="microphone">
    </service>

In my service launcher

     ContextCompat.startForegroundService(application, Intent(application, RecorderService::class.java))

Inside the actual service

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO)

    val notification = buildRecordingNotification()
    startServiceWithNotification(notification)

    recordingJob = recordSounds.executeCancellable(Unit, serviceScope)

    return START_STICKY
}

private fun buildRecordingNotification(): Notification {
    val channelId = buildNotificationChannelId()
    val builder = NotificationCompat.Builder(this, channelId)
    return builder
        .setSmallIcon(R.drawable.ic_stat_notify)
        .setContentTitle("Recording...")
        .setWhen(0)
        .setChannelId(channelId)
        .build()
}

private fun buildNotificationChannelId() =
    NotificationChannelHelper.createChannel(this, NotificationChannelHelper.NOTIF_ID_RECORDER, NotificationManagerCompat.IMPORTANCE_LOW)

private fun startServiceWithNotification(notification: Notification) =
    try {
        startForeground(NOTIFICATION_ID, notification)
    } catch (e: Exception) {
        Log.e("RecorderService", "Failed to startForeground", e)
    }

Inside my recording job

private suspend fun startRecorder() {
    audioRecord = withContext(Dispatchers.Main) {
        CustomAudioRecord().also { ar ->
            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO)
            ar.startRecording()
            recordingSessionId = UUID.randomUUID().toString()
        }
    }
}

The problem

On OnePlus phones, the foreground service dies after around 30 minutes (so does the app) when the screen is closed and app put into background (easiest way to reproduce)

What I have tried

  • I tried making a lightweight process to host the service, in which I inject only the needed dependencies for the service to run. The whole process takes up around 120mb of ram, compared to nearly 700mb when the main process is running.

  • I checked dumpsys logs and got the following reading on my service:

      Proc #25: prcp   F/S/FGS  --M  t: 0 22173:***Recorder/u0a714 (fg-service)
    

Looks like it checks out as being a correctly created foreground service.

  • I tried using android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO)

Didn't help.

  • I checked for wakelocks (tried using another one explicitly) and the Mic IN wakelock is active during my service.

A few notes

  • We have another foreground service for media playback, and it is usually not killed on OnePlus phones. It is built the same way.
  • On some devices the service runs all day all night without a problem.
0 Answers
Related