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

Kotlin으로 안드로이드에서 전화번호가 입력된 다이얼러 화면 여는 방법

이 예제에서는 Kotlin을 사용하여 안드로이드 앱에서 특정 전화번호가 미리 입력된 상태로 다이얼러(전화 걸기 화면)를 여는 방법을 알아봅니다.

구현 단계

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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:text="Tutorials Point"
    android:textAlignment="center"
    android:textColor="@android:color/holo_green_dark"
    android:textSize="32sp"
    android:textStyle="bold" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginTop="40dp"
    android:onClick="showDialer"
    android:text="Show Number Dialer" />
</RelativeLayout>

3단계 − src/MainActivity.kt 파일에 아래 코드를 추가합니다.

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
    }
    fun showDialer(view: View) {
        val intent = Intent(Intent.ACTION_DIAL)
        intent.data = Uri.parse("tel:0123456789")
        startActivity(intent)
    }
}

여기서 핵심은 Intent(Intent.ACTION_DIAL)을 사용하는 것입니다. ACTION_DIAL 인텐트는 별도의 권한 없이도 기본 다이얼러 앱을 실행하며, Uri.parse("tel:0123456789")로 지정한 전화번호가 자동으로 입력된 상태로 화면이 열립니다.

4단계 − AndroidManifest.xml 파일에 아래 코드를 추가합니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11">
    <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 Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바에서 실행(Run) 아이콘을 클릭하세요. 옵션 목록에서 사용 중인 모바일 기기를 선택하면, 앱이 기기에 설치되어 기본 화면이 표시됩니다.

Kotlin으로 안드로이드에서 전화번호가 입력된 다이얼러 화면 여는 방법

버튼을 누르면 지정한 전화번호가 입력된 다이얼러 화면이 아래와 같이 나타납니다.

Kotlin으로 안드로이드에서 전화번호가 입력된 다이얼러 화면 여는 방법