Computer >> 컴퓨터 >  >> 프로그래밍 >> Android

Kotlin으로 Android 홈 버튼 및 기타 시스템 버튼 비활성화하기

이 튜토리얼에서는 Kotlin을 사용하여 Android 앱에서 홈(Home) 버튼과 네비게이션 바 등 시스템 버튼을 비활성화하는 방법을 단계별로 살펴봅니다.

1단계 — 새 프로젝트 생성

Android Studio를 실행한 뒤 File → New Project 메뉴로 이동하여 새 프로젝트를 생성하고, 필요한 모든 세부 정보를 입력합니다.

2단계 — 레이아웃 파일 작성

다음 코드를 res/layout/activity_main.xml에 추가합니다.

예제

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:padding="8dp"
        android:text="Tutorials Point"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="48sp"
        android:textStyle="bold" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Disabled Home and Other System buttons"
        android:textAlignment="center"
        android:textColor="@android:color/background_dark"
        android:textSize="24sp"
        android:textStyle="bold" />
</RelativeLayout>

3단계 — MainActivity.kt 작성

다음 코드를 src/MainActivity.kt에 추가합니다. 핵심은 onWindowFocusChanged() 콜백에서 네비게이션 바를 숨기는 hideNavigationBar() 메서드를 호출하는 것입니다.

예제

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import java.util.*
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
    }
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        hideNavigationBar()
    }
    private fun hideNavigationBar() {
        val decorView: View = this.window.decorView
        val uiOptions: Int = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            or View.SYSTEM_UI_FLAG_FULLSCREEN
            or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
        val timer = Timer()
        val task: TimerTask = object : TimerTask() {
            override fun run() {
                runOnUiThread { decorView.systemUiVisibility = uiOptions }
            }
        }
        timer.scheduleAtFixedRate(task, 1, 2)
    }
}

참고: 이 코드는 SYSTEM_UI_FLAG_HIDE_NAVIGATION, SYSTEM_UI_FLAG_FULLSCREEN 등의 플래그를 조합하여 시스템 UI를 숨기고, Timer를 통해 주기적으로 플래그를 다시 적용함으로써 사용자가 버튼을 눌러도 네비게이션 바가 나타나지 않도록 유지합니다.

4단계 — AndroidManifest.xml 작성

다음 코드를 androidManifest.xml에 추가합니다.

예제

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.q1">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

앱 실행 및 결과 확인

이제 애플리케이션을 실행해 보겠습니다. 실제 Android 기기를 컴퓨터에 연결했다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바의 Run 아이콘Kotlin으로 Android 홈 버튼 및 기타 시스템 버튼 비활성화하기을 클릭하세요. 실행 옵션 목록에서 자신의 모바일 기기를 선택하면, 해당 기본 화면이 아래와 같이 표시됩니다.

Kotlin으로 Android 홈 버튼 및 기타 시스템 버튼 비활성화하기