Short answer (updated November 2026): To record video with the screen off on Android, the camera must run inside a foreground service declared with
foregroundServiceType="camera", started while the app is visible, with a partial wake lock and a Camera2 session that records to a surface instead of an on-screen preview. Without that, Android releases the camera when the display sleeps.
The Problem
You want to record video on Android with the screen off. You open the Camera2 API documentation, set up a CameraDevice, create a CaptureSession, and start a repeating request. It works great — until the screen turns off and everything stops.
The camera gets released. Your capture session closes. Your recording is gone.
This is one of the most common frustrations Android developers face when building camera apps that need to run in the background. The OS aggressively reclaims camera resources when the screen turns off, and the default behavior of most camera implementations does not account for this.
Why It Happens
Android's power management system is designed to conserve battery. When the screen turns off, the system enters a low-power state and begins releasing resources. The camera is one of the first things to go because it's power-hungry hardware.
Additionally, since Android 9 (API 28), apps running in the background cannot access the camera at all; camera work has to happen either in the foreground or inside a foreground service. Android 11 tightened this further: a foreground service started from the background still cannot use the camera, so start the service while your activity is on screen.
The Solution: Foreground Service + Wake Lock
The key to keeping the camera alive with the screen off is a combination of two things:
A foreground service — This keeps your app in a foreground state even when the UI is not visible. Android requires you to show a persistent notification, which is actually useful for camera recording apps (you want the user to know recording is active).
A partial wake lock — This prevents the CPU from sleeping, which keeps your camera capture pipeline running.
Here is the basic structure:
class CameraService : Service() {
private lateinit var wakeLock: PowerManager.WakeLock
private var cameraDevice: CameraDevice? = null
override fun onCreate() {
super.onCreate()
val powerManager = getSystemService(POWER_SERVICE) as PowerManager
wakeLock = powerManager.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK,
"MyApp::CameraWakeLock"
)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val notification = createNotification()
startForeground(NOTIFICATION_ID, notification)
wakeLock.acquire()
openCamera()
return START_STICKY
}
override fun onDestroy() {
wakeLock.release()
closeCamera()
super.onDestroy()
}
}
Camera2 API Setup Inside the Service
The camera setup inside the service follows the standard Camera2 pattern, but with a few important differences:
Use a SurfaceTexture instead of a preview surface. Since there is no visible preview (the screen is off), you cannot use a SurfaceView or TextureView. Instead, create a SurfaceTexture programmatically:
val surfaceTexture = SurfaceTexture(0)
surfaceTexture.setDefaultBufferSize(width, height)
val surface = Surface(surfaceTexture)
For recording, use MediaRecorder or MediaCodec. MediaRecorder is simpler for file-based recording. MediaCodec gives you more control for streaming scenarios.
val mediaRecorder = MediaRecorder().apply {
setAudioSource(MediaRecorder.AudioSource.MIC)
setVideoSource(MediaRecorder.VideoSource.SURFACE)
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
setOutputFile(outputPath)
setVideoEncoder(MediaRecorder.VideoEncoder.H264)
setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
setVideoSize(width, height)
setVideoFrameRate(30)
prepare()
}
Create the capture session with both surfaces:
cameraDevice?.createCaptureSession(
listOf(recorderSurface, previewSurface),
object : CameraCaptureSession.StateCallback() {
override fun onConfigured(session: CameraCaptureSession) {
val request = cameraDevice!!.createCaptureRequest(
CameraDevice.TEMPLATE_RECORD
).apply {
addTarget(recorderSurface)
}
session.setRepeatingRequest(request.build(), null, null)
mediaRecorder.start()
}
override fun onConfigureFailed(session: CameraCaptureSession) {
// Handle failure
}
},
backgroundHandler
)
Manufacturer-Specific Gotchas
This is where things get messy. Different Android manufacturers handle background camera access differently:
Samsung devices may kill foreground services after extended periods. You need to handle service restart and camera reconnection gracefully.
Xiaomi/MIUI has aggressive battery optimization that can kill foreground services. Users may need to manually whitelist your app in battery settings.
Huawei/EMUI has similar aggressive battery management. The app needs to be added to the "protected apps" list.
OnePlus/OxygenOS generally works well with foreground services but may throttle camera frame rates in the background.
The solution is to provide clear instructions in your app for users on these devices, and to handle camera disconnection and reconnection robustly.
Handling Camera Disconnection
Even with a foreground service and wake lock, the camera can be disconnected by the system or by another app claiming camera access. You need to handle this in your CameraDevice.StateCallback:
override fun onDisconnected(camera: CameraDevice) {
// Camera was taken by another app or the system
// Attempt to reopen after a short delay
handler.postDelayed({
openCamera()
}, 1000)
}
override fun onError(camera: CameraDevice, error: Int) {
// Handle specific error codes
when (error) {
CameraDevice.StateCallback.ERROR_CAMERA_IN_USE -> {
// Another app has the camera, retry later
}
CameraDevice.StateCallback.ERROR_CAMERA_DEVICE -> {
// Camera hardware error, may need device restart
}
}
}
Permissions and Manifest Setup
Your manifest needs several permissions and the foreground service declaration:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA" />
<service
android:name=".CameraService"
android:foregroundServiceType="camera"
android:exported="false" />
Apps targeting Android 11 (API 30) or later must declare foregroundServiceType="camera" for a foreground service that uses the camera, and pass ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA to startForeground(). Android 14 (API 34) goes further: every foreground service must declare a type (otherwise MissingForegroundServiceTypeException), and a camera service also needs the FOREGROUND_SERVICE_CAMERA permission plus the runtime CAMERA grant, or startForeground() throws a SecurityException.
Battery Considerations
Turning the display off removes one of the largest power draws on a phone, so screen-off recording uses noticeably less battery than recording with the screen on. Actual drain varies widely with the device, its battery health, resolution, encoder, and which radios are active, so measure on your own hardware rather than trusting a single figure. Additional savings come from disabling radios (Wi-Fi, cellular, Bluetooth) if your use case allows it, and for an always-on camera, keep the phone on a charger.
What I Built With This
I used this approach to build Background Camera RemoteStream, an Android app that records video with the screen completely off and includes remote control via an embedded web server. The app is free on Google Play for basic recording.
If you are building something similar, feel free to ask questions in the comments. The Camera2 API has a lot of edge cases, especially around background operation.
Also related: keeping the recording alive is only half the job — Why an Android Phone Can Record All Night and Still Be Unreachable covers what happens to the Wi-Fi radio once the screen is off.
Related guides
- Best Android apps for recording video with the screen off (2026 comparison)
- How an Android phone keeps recording with the screen off: foreground services, Camera2, and the OS fighting you
- Turn an old Android phone into a screen-off security camera (no cloud, LAN-only)
- Streaming to YouTube Live from an Android phone with the screen off
Reviewed November 2, 2026: Android version requirements corrected against the Android Developers foreground-service documentation; unmeasured battery figures removed.
Top comments (0)