본문 바로가기
Android

Android 잠금화면 lockscreen 만드는 방법

by kkong93 2023. 5. 20.
반응형

 

잠금화면 만들기 (코드 참고용)

class LockScreenService : Service() {

    private val screenOffReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            if (intent.action == Intent.ACTION_SCREEN_OFF || intent.action == Intent.ACTION_USER_PRESENT) {
                val lockIntent = Intent(context, LockScreenActivity::class.java)
                lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                context.startActivity(lockIntent)
            }
        }
    }

    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        val filter = IntentFilter(Intent.ACTION_SCREEN_OFF)
        filter.addAction(Intent.ACTION_USER_PRESENT)
        registerReceiver(screenOffReceiver, filter)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationChannel = NotificationChannel(CHANNEL_ID, "My Service", NotificationManager.IMPORTANCE_DEFAULT)
            val notificationManager = getSystemService(NotificationManager::class.java)
            notificationManager.createNotificationChannel(notificationChannel)

            val notification = Notification.Builder(this, CHANNEL_ID)
                .setContentTitle("My Service")
                .setContentText("Service is running...")
                .build()

            startForeground(1, notification)
        }
    }

//서비스가 종료될때 정리할 리소스 넣어주기
    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(screenOffReceiver)
    }
}

잠금화면을 표시하려면 Activity위에 Window에 특정 플래그를 설정해야한다.

이 플래그는 잠금 화면 위에 표시되도록 하며, 자금 화면이 해제되지 않아도 액티비티와 상호작용할 수 있게 한다.

 

 

잠금화면 Activity

class LockScreenActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_lock_screen)

        // Make the activity show over the lock screen and wake up the device
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            setShowWhenLocked(true)
            setTurnScreenOn(true)
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
        }
    }
}

 

메인에서 실행해주기

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

       if (!isMyServiceRunning(LockScreenService::class.java)) {
        val serviceIntent = Intent(this, LockScreenService::class.java)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(serviceIntent)
        } else {
            startService(serviceIntent)
        }
    }

    }

//서비스가 실행중인지 판단
    private fun isMyServiceRunning(serviceClass: Class<*>): Boolean {
        val manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        for (service in manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.name == service.service.className) {
                return true
            }
        }
        return false
    }
}

 

 

 서비스 해제는 MainActivity에서 해주는것이 적절하다

 val stopButton = findViewById<Button>(R.id.stopButton)
        stopButton.setOnClickListener {
            stopService(Intent(this, LockScreenService::class.java))
        }

버튼 눌렀을때 서비스 stop해주기

 

반응형

댓글