I'm working on an Android app and want to Customize Lock Screen UI using Kotlin. I’ve tried using the KeyguardManager
API, but I’m not able to change the lock screen background or add widgets.
What I’ve Tried:
I attempted to use KeyguardManager
and WindowManager
but couldn’t override the default lock screen. Here’s my code so far:
kotlin
class LockScreenService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
val keyguardLock = keyguardManager.newKeyguardLock("MyLock")
keyguardLock.disableKeyguard() // Attempt to disable default lock screen
val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
val params = WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
PixelFormat.TRANSLUCENT
)
val customView = LayoutInflater.from(this).inflate(R.layout.custom_lock_screen, null)
windowManager.addView(customView, params) // Attempt to overlay a custom view
return START_STICKY
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
}
Problem:
The default lock screen is still appearing even after calling
disableKeyguard()
.The custom lock screen sometimes disappears when unlocking the device.
I’m unsure how to set a custom wallpaper for the lock screen programmatically.
Expected Outcome:
Completely replace the default lock screen with a custom UI.
Show a widget on the lock screen.
Set a custom wallpaper on the lock screen.
Is there an alternative method or API to achieve this? Any guidance would be helpful!