Implementing a Custom Lock Screen on Android: Services, Broadcasts, UI Flags, Immersive Mode, and Fingerprint Unlock
Developers can create a custom Android lock screen by starting a foreground service that registers a dynamic SCREEN_OFF broadcast, launching a lock‑screen activity with keyguard‑dismiss and immersive window flags, handling swipe‑to‑unlock gestures, making navigation bars transparent, and integrating fingerprint authentication while respecting system security constraints.
Lock screens have existed since the early days of mobile phones and remain essential for privacy, accidental‑tap prevention, and power saving. While the built‑in lock screen satisfies most needs, developers may want a custom lock screen to provide richer interactions, such as music controls or lyric display, reducing the user workflow from several steps to just two: wake the screen and change the track.
Basic principle : A foreground Service is started when the app launches. The service registers a dynamic receiver for the Intent.ACTION_SCREEN_OFF broadcast. When the screen turns off, the receiver launches a lock‑screen Activity that is placed on top of the system lock screen (the system lock can only be dismissed if no password is set). The activity uses window flags to appear above the lock screen.
1. Broadcast registration
The service must register the SCREEN_OFF receiver dynamically; static registration in AndroidManifest.xml will not receive the broadcast. Example registration code (shown in the original article as an image) typically looks like:
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(screenOffReceiver, filter);The corresponding BroadcastReceiver is defined in code (image in the source) and starts the lock‑screen activity with the flag Intent.FLAG_ACTIVITY_NEW_TASK to avoid the runtime exception "Calling startActivity() from outside of an Activity".
Additional flags such as FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS can be used to keep the activity out of the recent‑apps list, though they are optional.
2. Activity configuration
The lock‑screen activity must be able to show over the system lock screen and optionally dismiss it. The older approach uses KeyguardManager :
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock lock = km.newKeyguardLock("MyLock");
lock.disableKeyguard();Since this method is deprecated, the modern way is to set window flags in onCreate() :
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);Remember to declare the appropriate permission android.permission.DISABLE_KEYGUARD in the manifest.
3. Key handling
To prevent the user from exiting the custom lock screen with hardware keys, the activity can override onKeyDown() for KEYCODE_BACK and KEYCODE_MENU . Hiding the Home and Recent keys requires more invasive techniques (reflection or window type changes) and is generally discouraged because it may cause instability.
4. Swipe‑to‑unlock
The UI contains a full‑screen UnderView placed beneath the content view ( mMoveView ). All touch events are intercepted in UnderView.onTouchEvent() . The logic records the start X coordinate ( mStartX ), moves the content with mMoveView.setTranslationX() , and on release decides whether the swipe distance exceeds 0.4 * screenWidth . If the threshold is met, an ObjectAnimator slides the view off‑screen and finishes the activity; otherwise it animates back to the original position.
float deltaX = event.getX() - mStartX;
mMoveView.setTranslationX(deltaX);
// on release
if (Math.abs(deltaX) > 0.4f * screenWidth) {
// animate out and finish
} else {
// animate back
}During the animation the background alpha is adjusted to create a dimming effect.
5. Immersive mode and transparent bars
Android 4.4 introduced SYSTEM_UI_FLAG_IMMERSIVE and SYSTEM_UI_FLAG_IMMERSIVE_STICKY for true full‑screen experiences. Combined with flags such as SYSTEM_UI_FLAG_LAYOUT_STABLE , SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION , SYSTEM_UI_FLAG_HIDE_NAVIGATION , and SYSTEM_UI_FLAG_FULLSCREEN , developers can hide the navigation bar while keeping the status bar visible, or hide both.
Transparent (translucent) bars are achieved by adding FLAG_TRANSLUCENT_STATUS (pre‑Lollipop) or FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS together with setStatusBarColor(Color.TRANSPARENT) on Lollipop and above. The article demonstrates applying both immersive mode and transparent bars to the custom lock screen, resulting in a sleek UI where the navigation bar briefly appears on a swipe and then fades.
6. Fingerprint unlock integration
When a device uses fingerprint authentication, the custom lock screen may remain visible after the system unlocks. The solution is to listen for Intent.ACTION_USER_PRESENT in the service and finish the lock‑screen activity only if a lock‑screen password is set ( KeyguardManager.isKeyguardSecure() ).
if (km.isKeyguardSecure()) {
// handle broadcast and finish activity
}For devices where fingerprint unlock does not work under a custom lock screen (e.g., Xiaomi), the activity can directly invoke the fingerprint API:
FingerprintManager fm = (FingerprintManager) getSystemService(Context.FINGERPRINT_SERVICE);
if (fm.isHardwareDetected() && fm.hasEnrolledFingerprints()) {
fm.authenticate(null, 0, null, authCallback, null);
}The callback’s onAuthenticationSucceeded() calls finish() to dismiss the custom lock screen.
Conclusion
The article provides a comprehensive guide for Android developers to implement a custom lock screen, covering service‑based activation, dynamic broadcast registration, window flag manipulation, touch‑event handling for swipe unlock, immersive mode and transparent‑bar configuration, and fingerprint authentication integration. It emphasizes careful handling of system restrictions (e.g., password‑protected lock screens) and encourages clean, maintainable code.
Tencent Music Tech Team
Public account of Tencent Music's development team, focusing on technology sharing and communication.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.