Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Mở Dialer với số điện thoại có sẵn Android Kotlin

Hướng dẫn này minh họa cách mở ứng dụng Dialer (keypad gọi điện) với số điện thoại được điền sẵn trong Android sử dụng Kotlin.

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.

Bước 2: Thiết kế giao diện (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>

Bước 3: Viết code 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)
    }
}

Bước 4: Cấu hình 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>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. Nhấn nút "Show Number Dialer" → Mở ứng dụng Phone với số 0123456789 đã điền sẵn. Người dùng chỉ cần nhấn nút gọi.

Giải thích Intent.ACTION_DIAL vs ACTION_CALL

ActionHành viQuyền cần
ACTION_DIALMở dialer, số điền sẵn, user tự nhấn gọiKhông cần quyền
ACTION_CALLGọi trực tiếp không qua dialerCALL_PHONE (dangerous)
Khuyến nghị: Luôn dùng ACTION_DIAL để tôn trọng quyền riêng tư người dùng và tránh yêu cầu quyền nhạy cảm.

Xử lý số điện thoại động

fun callNumber(phoneNumber: String) {
    val cleanNumber = phoneNumber.replace("\\D".toRegex(), "") // Chỉ giữ số
    val intent = Intent(Intent.ACTION_DIAL).apply {
        data = Uri.parse("tel:$cleanNumber")
    }
    if (intent.resolveActivity(packageManager) != null) {
        startActivity(intent)
    }
}

Các URI scheme liên quan

  • tel:0123456789 - Gọi điện thoại
  • sms:0123456789 - Mở app nhắn tin
  • smsto:0123456789?body=Hello - Nhắn tin có nội dung sẵn
  • mailto:email@example.com - Gửi email