Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีเปิด Dialer พร้อมหมายเลขโทรศัพท์ใน Android ด้วย Kotlin

ตัวอย่างนี้สาธิตวิธี เปิดแอปโทรศัพท์ (Dialer) พร้อมกรอกหมายเลขให้ล่วงหน้า โดยใช้ Intent.ACTION_DIAL ซึ่งปลอดภัยและไม่ต้องขอ Permission

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

Android Studio → New Project → Empty Activity

ขั้นตอนที่ 2: 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="Open Dialer Demo"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnDial"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginTop="40dp"
        android:onClick="openDialer"
        android:text="Open Dialer with Number" />

</RelativeLayout>

ขั้นตอนที่ 3: โค้ดหลัก (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 = "Dialer Demo"
    }

    fun openDialer(view: View) {
        // 🔑 ใช้ ACTION_DIAL - เปิด Dialer พร้อมหมายเลข แต่ไม่โทรออกเลย
        val intent = Intent(Intent.ACTION_DIAL).apply {
            data = Uri.parse("tel:0123456789")
        }

        // ตรวจสอบว่ามีแอปรับโทรศัพท์หรือไม่
        if (intent.resolveActivity(packageManager) != null) {
            startActivity(intent)
        } else {
            // Fallback: ไม่มี Dialer app
            // Toast.makeText(this, "No dialer app found", Toast.LENGTH_SHORT).show()
        }
    }
}

📞 ACTION_DIAL vs ACTION_CALL

Action พฤติกรรม Permission ใช้เมื่อไหร่
ACTION_DIAL เปิด Dialer กรอกหมายเลขให้ → ผู้ใช้กดโทรเอง ❌ ไม่ต้อง แอปทั่วไป (แนะนำ)
ACTION_CALL โทรออกทันทีโดยไม่ผ่าน Dialer ✅ CALL_PHONE (Dangerous) แอปโทรศัพท์ตัวแทน, Emergency

📱 รูปแบบหมายเลขที่รองรับ

// ธรรมดา
"tel:0123456789"

// พร้อม Country Code
"tel:+66812345678"

// พร้อม Pause (,) และ Wait (;) - สำหรับ Extension
"tel:0123456789,1234"      // Pause 2 วิ แล้วส่ง 1234
"tel:0123456789;1234"      // Wait ผู้ใช้กด OK แล้วค่อยส่ง 1234

// USSD Code
"tel:*123%23"  // *123# (ต้อง Encode # → %23)

🔧 เพิ่ม Chooser (ให้ผู้ใช้เลือกแอปโทร)

val chooser = Intent.createChooser(intent, "Call with:")
startActivity(chooser)

ขั้นตอนที่ 4: AndroidManifest.xml

ไม่ต้องเพิ่ม Permission ใดๆ สำหรับ ACTION_DIAL

<!-- สำหรับ ACTION_CALL เท่านั้น -->
<!-- <uses-permission android:name="android.permission.CALL_PHONE" /> -->

ทดสอบ

  1. รันแอป → กดปุ่ม "Open Dialer with Number"
  2. จะเปิดแอปโทรศัพท์พร้อมหมายเลข 0123456789
  3. ผู้ใช้ต้องกดปุ่มโทร (สีเขียว) เอง